单军华
2018-07-11 7b02207537d35bfa1714bf8beafc921f717d100a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//
//  ZJContentView.m
//  ZJScrollPageView
//
//  Created by jasnig on 16/5/6.
//  Copyright © 2016年 ZeroJ. All rights reserved.
//
 
#import "ZJContentView.h"
#import "ZJScrollSegmentView.h"
#import "UIViewController+ZJScrollPageController.h"
 
typedef NS_ENUM(NSInteger, ZJScrollPageControllerScrollDirection) {
    ZJScrollPageControllerScrollDirectionNone,
    ZJScrollPageControllerScrollDirectionLeft,
    ZJScrollPageControllerScrollDirectionRight
};
 
@interface ZJContentView ()<UIScrollViewDelegate, UIGestureRecognizerDelegate> {
    CGFloat   _oldOffSetX;
    BOOL _isLoadFirstView;
//    BOOL _isAnimating;
}
/** 避免循环引用*/
@property (weak, nonatomic) ZJScrollSegmentView *segmentView;
 
 
// 父类 用于处理添加子控制器  使用weak避免循环引用
@property (weak, nonatomic) UIViewController *parentViewController;
// 当这个属性设置为YES的时候 就不用处理 scrollView滚动的计算
@property (assign, nonatomic) BOOL forbidTouchToAdjustPosition;
@property (assign, nonatomic) NSInteger itemsCount;
// 所有的子控制器
@property (strong, nonatomic) NSMutableDictionary<NSString *, UIViewController<ZJScrollPageViewChildVcDelegate> *> *childVcsDic;
// 当前控制器
@property (strong, nonatomic) UIViewController<ZJScrollPageViewChildVcDelegate> *currentChildVc;
 
/// 如果类似cell缓存一样, 虽然创建的控制器少了, 但是每个页面每次都要重新加载数据, 否则显示的内容就会出错, 貌似还不如每个页面创建一个控制器好
//@property (strong, nonatomic) NSCache *cacheChildVcs;
 
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIView *currentView;
@property (strong, nonatomic) UIView *oldView;
@property (assign, nonatomic) NSInteger currentIndex;
@property (assign, nonatomic) NSInteger oldIndex;
@property (assign, nonatomic) ZJScrollPageControllerScrollDirection scrollDirection;
 
@end
 
@implementation ZJContentView
#define cellID @"cellID"
 
static NSString *const kContentOffsetOffKey = @"contentOffset";
#pragma mark - life cycle
 
- (instancetype)initWithFrame:(CGRect)frame segmentView:(ZJScrollSegmentView *)segmentView parentViewController:(UIViewController *)parentViewController delegate:(id<ZJScrollPageViewDelegate>) delegate {
    
    if (self = [super initWithFrame:frame]) {
        self.segmentView = segmentView;
        self.delegate = delegate;
        self.parentViewController = parentViewController;
 
        [self commonInit];
        [self addNotification];
    }
    return self;
}
 
- (void)commonInit {
    
    _oldIndex = -1;
    _currentIndex = -1;
    _oldOffSetX = 0.0f;
    _forbidTouchToAdjustPosition = NO;
    _isLoadFirstView = YES;
    if ([_delegate respondsToSelector:@selector(numberOfChildViewControllers)]) {
        self.itemsCount = [_delegate numberOfChildViewControllers];
    }
    [self addSubview:self.scrollView];
    
    [self setCurrentIndex:0 andScrollDirection:ZJScrollPageControllerScrollDirectionNone];
 
    if (self.parentViewController.parentViewController && [self.parentViewController.parentViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *navi = (UINavigationController *)self.parentViewController.parentViewController;
        
        if (navi.interactivePopGestureRecognizer) {
            navi.interactivePopGestureRecognizer.delegate = self;
            [self.scrollView.panGestureRecognizer requireGestureRecognizerToFail:navi.interactivePopGestureRecognizer];
        }
    }
}
 
- (void)addNotification {
 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveMemoryWarningHander:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
}
 
- (void)receiveMemoryWarningHander:(NSNotificationCenter *)noti {
    
    __weak typeof(self) weakSelf = self;
    [_childVcsDic enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, UIViewController<ZJScrollPageViewChildVcDelegate> * _Nonnull childVc, BOOL * _Nonnull stop) {
        __strong typeof(self) strongSelf = weakSelf;
 
        if (childVc != strongSelf.currentChildVc) {
            [_childVcsDic removeObjectForKey:key];
            [ZJContentView removeChildVc:childVc];
        }
 
    }];
 
}
 
- (void)dealloc {
    self.parentViewController = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
#if DEBUG
    NSLog(@"ZJContentView---销毁");
#endif
}
 
#pragma mark - public helper
 
/** 给外界可以设置ContentOffSet的方法 */
- (void)setContentOffSet:(CGPoint)offset animated:(BOOL)animated {
    self.forbidTouchToAdjustPosition = YES;
    
    NSInteger currentIndex = offset.x/self.scrollView.bounds.size.width;
    _oldIndex = _currentIndex;
    [self setCurrentIndex:currentIndex andScrollDirection:ZJScrollPageControllerScrollDirectionNone];
    
    if (animated) {
        CGFloat delta = offset.x - self.scrollView.contentOffset.x;
        NSInteger page = fabs(delta)/self.scrollView.bounds.size.width;
        if (page>=2) {// 需要滚动两页以上的时候, 跳过中间页的动画
            
            __weak typeof(self) weakself = self;
            dispatch_async(dispatch_get_main_queue(), ^{
                __strong typeof(weakself) strongSelf = weakself;
                if (strongSelf) {
                    [strongSelf.scrollView setContentOffset:offset animated:NO];
                    [self didAppearWithIndex:currentIndex];
                    [self didDisappearWithIndex:_oldIndex];
                }
            });
        }
        else {
            [self.scrollView setContentOffset:offset animated:animated];
            [self didAppearWithIndex:currentIndex];
            [self didDisappearWithIndex:_oldIndex];
 
        }
    }
    else {
        [self.scrollView setContentOffset:offset animated:animated];
        [self didAppearWithIndex:currentIndex];
        [self didDisappearWithIndex:_oldIndex];
 
 
    }
    
}
 
/** 给外界刷新视图的方法 */
- (void)reload {
    
    [self.childVcsDic enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, UIViewController<ZJScrollPageViewChildVcDelegate> * _Nonnull childVc, BOOL * _Nonnull stop) {
        [ZJContentView removeChildVc:childVc];
        childVc = nil;
 
    }];
    self.childVcsDic = nil;
    [self.scrollView removeFromSuperview];
    self.scrollView = nil;
    [self commonInit];
}
 
+ (void)removeChildVc:(UIViewController *)childVc {
    [childVc willMoveToParentViewController:nil];
    [childVc.view removeFromSuperview];
    [childVc removeFromParentViewController];
}
 
#pragma mark - UIScrollViewDelegate
 
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.forbidTouchToAdjustPosition) {// first or last
        return;
    }
    
    if (scrollView.contentOffset.x <= 0) {
        [self contentViewDidMoveFromIndex:0 toIndex:0 progress:1.0];
        return;
 
    }
    if (scrollView.contentOffset.x >= scrollView.contentSize.width - scrollView.bounds.size.width) {
        [self contentViewDidMoveFromIndex:_itemsCount-1 toIndex:_itemsCount-1 progress:1.0];
        return;
 
    }
    
    CGFloat tempProgress = scrollView.contentOffset.x / self.bounds.size.width;
    NSInteger tempIndex = (NSInteger)tempProgress;
    CGFloat progress = tempProgress - floor(tempProgress);
    CGFloat deltaX = scrollView.contentOffset.x - _oldOffSetX;
    
    if (deltaX > 0 && (deltaX != scrollView.bounds.size.width)) {// 向右
        _oldIndex = tempIndex;
        NSInteger tempCurrentIndex = tempIndex + 1;
        
        [self setCurrentIndex:tempCurrentIndex andScrollDirection:ZJScrollPageControllerScrollDirectionRight];
 
    }
    else if (deltaX < 0) {
        progress = 1.0 - progress;
 
         _oldIndex = tempIndex + 1;
        
         [self setCurrentIndex:tempIndex andScrollDirection:ZJScrollPageControllerScrollDirectionLeft];
 
    }
    else {
         return;
    }
 
    NSLog(@"%f------%ld----%ld------", progress, _oldIndex, _currentIndex);
    
    [self contentViewDidMoveFromIndex:_oldIndex toIndex:_currentIndex progress:progress];
 
}
 
/** 滚动减速完成时再更新title的位置 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger currentIndex = (scrollView.contentOffset.x / self.bounds.size.width);
    if (_scrollDirection == ZJScrollPageControllerScrollDirectionNone && !_forbidTouchToAdjustPosition) { // 开启bounds 在第一页和最后一页快速松开手又接触滑动的时候 会不合理的被调用这个代理方法 ---- 其实这个时候并没有在松开手的情况下减速完成
        return;
    }
    [self contentViewDidMoveFromIndex:currentIndex toIndex:currentIndex progress:1.0];
    // 调整title
    [self adjustSegmentTitleOffsetToCurrentIndex:currentIndex];
 
    if (scrollView.contentOffset.x == _oldOffSetX) {// 滚动未完成
 
        [self didAppearWithIndex:currentIndex];
        [self didDisappearWithIndex:_currentIndex];
    }
    else {
        [self didAppearWithIndex:_currentIndex];
        [self didDisappearWithIndex:_oldIndex];
    }
    // 重置_currentIndex 不触发set方法
    _currentIndex = currentIndex;
    _scrollDirection = ZJScrollPageControllerScrollDirectionNone;
 
}
 
 
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    _oldOffSetX = scrollView.contentOffset.x;
    self.forbidTouchToAdjustPosition = NO;
    _scrollDirection = ZJScrollPageControllerScrollDirectionNone;
    
}
 
 
#pragma mark - private helper
- (void)contentViewDidMoveFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex progress:(CGFloat)progress {
    if(self.segmentView) {
        [self.segmentView adjustUIWithProgress:progress oldIndex:fromIndex currentIndex:toIndex];
    }
}
 
- (void)adjustSegmentTitleOffsetToCurrentIndex:(NSInteger)index {
    if(self.segmentView) {
        [self.segmentView adjustTitleOffSetToCurrentIndex:index];
    }
    
}
 
- (void)willAppearWithIndex:(NSInteger)index {
    UIViewController<ZJScrollPageViewChildVcDelegate> *controller = [self.childVcsDic valueForKey:[NSString stringWithFormat:@"%ld", index]];
    if (controller) {
        if ([controller respondsToSelector:@selector(zj_viewWillAppearForIndex:)]) {
            [controller zj_viewWillAppearForIndex:index];
        }
        
        if (_delegate && [_delegate respondsToSelector:@selector(scrollPageController:childViewControllWillAppear:forIndex:)]) {
            [_delegate scrollPageController:self.parentViewController childViewControllWillAppear:controller forIndex:index];
        }
    }
    
    
}
 
- (void)didAppearWithIndex:(NSInteger)index {
    UIViewController<ZJScrollPageViewChildVcDelegate> *controller = [self.childVcsDic valueForKey:[NSString stringWithFormat:@"%ld", index]];
    if (controller) {
        if ([controller respondsToSelector:@selector(zj_viewDidAppearForIndex:)]) {
            [controller zj_viewDidAppearForIndex:index];
        }
        
        if (_delegate && [_delegate respondsToSelector:@selector(scrollPageController:childViewControllDidAppear:forIndex:)]) {
            [_delegate scrollPageController:self.parentViewController childViewControllDidAppear:controller forIndex:index];
        }
    }
    
    
    
}
 
- (void)willDisappearWithIndex:(NSInteger)index {
    UIViewController<ZJScrollPageViewChildVcDelegate> *controller = [self.childVcsDic valueForKey:[NSString stringWithFormat:@"%ld", index]];
    if (controller) {
        if ([controller respondsToSelector:@selector(zj_viewWillDisappearForIndex:)]) {
            [controller zj_viewWillDisappearForIndex:index];
        }
        if (_delegate && [_delegate respondsToSelector:@selector(scrollPageController:childViewControllWillDisappear:forIndex:)]) {
            [_delegate scrollPageController:self.parentViewController childViewControllWillDisappear:controller forIndex:index];
        }
    }
    
}
- (void)didDisappearWithIndex:(NSInteger)index {
    UIViewController<ZJScrollPageViewChildVcDelegate> *controller = [self.childVcsDic valueForKey:[NSString stringWithFormat:@"%ld", index]];
    if (controller) {
        if ([controller respondsToSelector:@selector(zj_viewDidDisappearForIndex:)]) {
            [controller zj_viewDidDisappearForIndex:index];
        }
        if (_delegate && [_delegate respondsToSelector:@selector(scrollPageController:childViewControllDidDisappear:forIndex:)]) {
            [_delegate scrollPageController:self.parentViewController childViewControllDidDisappear:controller forIndex:index];
        }
    }
}
 
 
- (void)setCurrentIndex:(NSInteger)currentIndex andScrollDirection:(ZJScrollPageControllerScrollDirection)scrollDirection {
    if (currentIndex != _currentIndex) {
        
//        NSLog(@"current -- %ld   _current ---- %ld _oldIndex --- %ld", currentIndex, _currentIndex, _oldIndex);
        [self setupSubviewsWithCurrentIndex:currentIndex oldIndex:_oldIndex];
 
        if (scrollDirection != ZJScrollPageControllerScrollDirectionNone) {
            // 打开右边, 但是未松手又返回了打开左边
            // 打开左边, 但是未松手又返回了打开右边
            [self didDisappearWithIndex:_currentIndex];
 
        }
        [self willAppearWithIndex:currentIndex];
        [self willDisappearWithIndex:_oldIndex];
        if (_isLoadFirstView) { // 加载第一个controller的时候 同时出发didAppear
            [self didAppearWithIndex:currentIndex];
            _isLoadFirstView = NO;
        }
        
        _scrollDirection = scrollDirection;
        _currentIndex = currentIndex;
 
//        NSLog(@"%@",self.scrollView.subviews);
    }
    
}
 
- (void)setupSubviewsWithCurrentIndex:(NSInteger)currentIndex oldIndex:(NSInteger)oldIndex {
    UIViewController<ZJScrollPageViewChildVcDelegate> *currentController = [self.childVcsDic valueForKey:[NSString stringWithFormat:@"%ld", (long)currentIndex]];
    
    if (_delegate && [_delegate respondsToSelector:@selector(childViewController:forIndex:)]) {
        if (currentController == nil) {
            currentController = [_delegate childViewController:nil forIndex:currentIndex];
            [self.childVcsDic setValue:currentController forKey:[NSString stringWithFormat:@"%ld", (long)currentIndex]];
        } else {
            [_delegate childViewController:currentController forIndex:currentIndex];
        }
    } else {
        NSAssert(NO, @"必须设置代理和实现代理方法");
    }
 
    if ([currentController isKindOfClass:[UINavigationController class]]) {
        NSAssert(NO, @"不要添加UINavigationController包装后的子控制器");
        
    }
    
    CGFloat width = self.bounds.size.width;
    CGFloat height = self.bounds.size.height;
    
    // 添加currentController
    if (currentController.zj_scrollViewController != self.parentViewController) {
        [self.parentViewController addChildViewController:currentController];
    }
    [self.currentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.currentView.frame = CGRectMake(currentIndex*width, 0, width, height);
    [self.currentView addSubview:currentController.view];
    [_currentChildVc didMoveToParentViewController:self.parentViewController];
    
    UIViewController *oldController = [self.childVcsDic valueForKey:[NSString stringWithFormat:@"%ld", (long)_oldIndex]];
    // 添加oldController
    if (oldController) {
 
        [self.oldView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        self.oldView.frame = CGRectMake(oldIndex*width, 0, width, height);
        [self.oldView addSubview:oldController.view];
        [oldController didMoveToParentViewController:self.parentViewController];
    }
    
    [self setNeedsLayout];
}
 
 
 
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (self.parentViewController.parentViewController && [self.parentViewController.parentViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *navi = (UINavigationController *)self.parentViewController.parentViewController;
        
        if (navi.visibleViewController == self.parentViewController) {// 当显示的是ScrollPageView的时候 只在第一个tag处执行pop手势
            
            return self.scrollView.contentOffset.x == 0;
        } else {
            return [super gestureRecognizerShouldBegin:gestureRecognizer];
        }
    }
    return [super gestureRecognizerShouldBegin:gestureRecognizer];
}
 
 
#pragma mark - getter --- setter
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
 
        self.currentView = [[UIView alloc] init];
        self.oldView = [[UIView alloc] init];
        scrollView.delegate = self;
        [scrollView addSubview:self.currentView];
        [scrollView addSubview:self.oldView];
        
        scrollView.pagingEnabled = YES;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.bounces = self.segmentView.segmentStyle.isContentViewBounces;
        scrollView.scrollEnabled = self.segmentView.segmentStyle.isScrollContentView;
        scrollView.contentSize = CGSizeMake(self.itemsCount*self.bounds.size.width, self.bounds.size.height);
        
        _scrollView = scrollView;
    }
    return _scrollView;
}
 
- (NSMutableDictionary<NSString *,UIViewController<ZJScrollPageViewChildVcDelegate> *> *)childVcsDic {
    if (!_childVcsDic) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        _childVcsDic = dic;
    }
    return _childVcsDic;
}
 
@end