@@ -26,7 +26,7 @@ @interface XYDebugWindow ()
2626
2727@property (nonatomic , strong ) UIScrollView *scrollView;
2828
29- @property (nonatomic , strong ) NSMutableArray <CALayer *>* debugLayers;
29+ @property (nonatomic , strong ) NSHashTable <CALayer *> * debugLayers;
3030
3131@end
3232
@@ -84,7 +84,7 @@ - (instancetype)initWithFrame:(CGRect)frame
8484 [self addSubview: _layerSlider];
8585 [self addSubview: _distanceSlider];
8686 self.backgroundColor = [UIColor clearColor ];
87- self.debugLayers = @[]. mutableCopy ;
87+ self.debugLayers = [ NSHashTable weakObjectsHashTable ] ;
8888 self.layer .masksToBounds = YES ;
8989 }
9090 return self;
@@ -116,17 +116,19 @@ - (void)setSouceView:(UIView *)souceView
116116 _souceView = souceView;
117117
118118 if (_souceView == nil ) {
119- [[_debugLayers copy ] makeObjectsPerformSelector: @selector (removeFromSuperlayer )];
120119 _scrollView.hidden = YES ;
121120 _layerSlider.hidden = YES ;
122121 _distanceSlider.hidden = YES ;
123122
124123 } else {
124+ [[self .debugLayers allObjects ] makeObjectsPerformSelector: @selector (removeFromSuperlayer )];
125+ [self .debugLayers removeAllObjects ];
125126 [self scrollViewAddLayersInView: _souceView layerLevel: 0 index: 0 ];
126127 _scrollView.contentOffset = CGPointMake (SCREEN_WIDTH /2.0 , SCREEN_HEIGHT /2.0 );
127128 _scrollView.hidden = NO ;
128129 _layerSlider.hidden = NO ;
129130 _distanceSlider.hidden = NO ;
131+ [self reCalculateZPostion ];
130132 [self recoverLayersDistance ];
131133 }
132134}
@@ -139,7 +141,7 @@ - (void)scrollViewAddLayersInView:(UIView *)view layerLevel:(CGFloat)layerLevel
139141 if (view.superview ) {
140142 UIView *cloneView = view.cloneView ;
141143 cloneView.layer .zPosition = 0 ;
142- cloneView.layer .debug_zPostion = ( layerLevel*80 + index* 8 ) - 600 ;
144+ cloneView.layer .debug_zPostion = layerLevel*20 + index;
143145 cloneView.layer .masksToBounds = YES ;
144146
145147 CGRect frame = [view.superview convertRect: view.frame toView: _souceView];
@@ -159,7 +161,7 @@ - (void)scrollViewAddLayersInView:(UIView *)view layerLevel:(CGFloat)layerLevel
159161
160162- (void )showDifferentLayers : (float )percent
161163{
162- CGFloat positionMax = self.debugLayers .firstObject .debug_zPostion ;
164+ CGFloat positionMax = self.debugLayers .anyObject .debug_zPostion ;
163165 CGFloat positionMin = positionMax;
164166 for (CALayer *layer in self.debugLayers ) {
165167 if (layer.debug_zPostion >= positionMax) {
@@ -199,6 +201,27 @@ - (void)changeDistance:(float)percent
199201 }
200202}
201203
204+ - (void )reCalculateZPostion
205+ {
206+ CGFloat positionMax = self.debugLayers .anyObject .debug_zPostion ;
207+ CGFloat positionMin = positionMax;
208+ for (CALayer *layer in self.debugLayers ) {
209+ if (layer.debug_zPostion >= positionMax) {
210+ positionMax = layer.debug_zPostion ;
211+ }
212+ if (layer.debug_zPostion <= positionMin) {
213+ positionMin = layer.debug_zPostion ;
214+ }
215+ }
216+
217+ CGFloat defalutMin = -600 ;
218+ CGFloat defalutMax = 400 ;
219+ CGFloat scale = (defalutMax-defalutMin)/(positionMax-positionMin);
220+ for (CALayer *layer in self.debugLayers ) {
221+ layer.debug_zPostion = defalutMin + (layer.debug_zPostion - positionMin)*scale;
222+ }
223+ }
224+
202225- (void )recoverLayersDistance
203226{
204227 _distanceSlider.defalutPercent = 0.5 ;
0 commit comments