@@ -26,7 +26,7 @@ @interface XYDebugWindow ()
26
26
27
27
@property (nonatomic , strong ) UIScrollView *scrollView;
28
28
29
- @property (nonatomic , strong ) NSMutableArray <CALayer *>* debugLayers;
29
+ @property (nonatomic , strong ) NSHashTable <CALayer *> * debugLayers;
30
30
31
31
@end
32
32
@@ -84,7 +84,7 @@ - (instancetype)initWithFrame:(CGRect)frame
84
84
[self addSubview: _layerSlider];
85
85
[self addSubview: _distanceSlider];
86
86
self.backgroundColor = [UIColor clearColor ];
87
- self.debugLayers = @[]. mutableCopy ;
87
+ self.debugLayers = [ NSHashTable weakObjectsHashTable ] ;
88
88
self.layer .masksToBounds = YES ;
89
89
}
90
90
return self;
@@ -116,17 +116,19 @@ - (void)setSouceView:(UIView *)souceView
116
116
_souceView = souceView;
117
117
118
118
if (_souceView == nil ) {
119
- [[_debugLayers copy ] makeObjectsPerformSelector: @selector (removeFromSuperlayer )];
120
119
_scrollView.hidden = YES ;
121
120
_layerSlider.hidden = YES ;
122
121
_distanceSlider.hidden = YES ;
123
122
124
123
} else {
124
+ [[self .debugLayers allObjects ] makeObjectsPerformSelector: @selector (removeFromSuperlayer )];
125
+ [self .debugLayers removeAllObjects ];
125
126
[self scrollViewAddLayersInView: _souceView layerLevel: 0 index :0 ];
126
127
_scrollView.contentOffset = CGPointMake (SCREEN_WIDTH/2.0 , SCREEN_HEIGHT/2.0 );
127
128
_scrollView.hidden = NO ;
128
129
_layerSlider.hidden = NO ;
129
130
_distanceSlider.hidden = NO ;
131
+ [self reCalculateZPostion ];
130
132
[self recoverLayersDistance ];
131
133
}
132
134
}
@@ -139,7 +141,7 @@ - (void)scrollViewAddLayersInView:(UIView *)view layerLevel:(CGFloat)layerLevel
139
141
if (view.superview ) {
140
142
UIView *cloneView = view.cloneView ;
141
143
cloneView.layer .zPosition = 0 ;
142
- cloneView.layer .debug_zPostion = ( layerLevel*80 + index * 8 ) - 600 ;
144
+ cloneView.layer .debug_zPostion = layerLevel*20 + index ;
143
145
cloneView.layer .masksToBounds = YES ;
144
146
145
147
CGRect frame = [view.superview convertRect: view.frame toView: _souceView];
@@ -159,7 +161,7 @@ - (void)scrollViewAddLayersInView:(UIView *)view layerLevel:(CGFloat)layerLevel
159
161
160
162
- (void )showDifferentLayers : (float )percent
161
163
{
162
- CGFloat positionMax = self.debugLayers .firstObject .debug_zPostion ;
164
+ CGFloat positionMax = self.debugLayers .anyObject .debug_zPostion ;
163
165
CGFloat positionMin = positionMax;
164
166
for (CALayer *layer in self.debugLayers ) {
165
167
if (layer.debug_zPostion >= positionMax) {
@@ -199,6 +201,27 @@ - (void)changeDistance:(float)percent
199
201
}
200
202
}
201
203
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
+
202
225
- (void )recoverLayersDistance
203
226
{
204
227
_distanceSlider.defalutPercent = 0.5 ;
0 commit comments