// // MLMSegmentScroll.m // MLMSegmentPage // // Created by my on 2017/2/6. // Copyright © 2017年 my. All rights reserved. // #import "MLMSegmentScroll.h" #import "SegmentPageHead.h" @interface MLMSegmentScroll () { CGFloat start_offset_x; } @property (nonatomic, strong) NSCache *viewsCache;//存储页面(使用计数功能) @property (nonatomic, strong) NSMutableArray *viewsArray; @end @implementation MLMSegmentScroll #pragma mark - init Method - (instancetype)initWithFrame:(CGRect)frame vcOrViews:(NSArray *)sources { if (self = [super initWithFrame:frame]) { _viewsArray = [sources mutableCopy]; [self defaultSet]; } return self; } #pragma mark - default setting - (void)defaultSet { WEAK(weakSelf, self) self.showsVerticalScrollIndicator = NO; self.showsHorizontalScrollIndicator = NO; self.pagingEnabled = YES; self.bounces = NO; self.delegate = weakSelf; [self setContentSize:CGSizeMake(_viewsArray.count *self.width, self.height)]; _countLimit = _viewsArray.count; } #pragma mark - viewsCache - (NSCache *)viewsCache { if (!_viewsCache) { WEAK(weakSelf, self) _viewsCache = [[NSCache alloc] init]; _viewsCache.countLimit = _countLimit; _viewsCache.delegate = weakSelf; _viewsCache.evictsObjectsWithDiscardedContent = YES; } return _viewsCache; } #pragma mark - default add View - (void)createView { _showIndex = MIN(_viewsArray.count-1, MAX(0, _showIndex)); [self setContentOffset:CGPointMake(_showIndex * self.frame.size.width, 0)]; if (_loadAll) { NSInteger startIndex; if (_viewsArray.count-_showIndex > _countLimit) { startIndex = _showIndex; } else { startIndex = _viewsArray.count - _countLimit; } for (NSInteger i = startIndex; i < startIndex + _countLimit; i ++) { [self addViewCacheIndex:i]; } } else { [self setContentOffset:CGPointMake(_showIndex*self.width, 0) animated:NO]; } } #pragma mark - changeSource - (void)changeSource:(NSArray *)sources { _viewsArray = [sources mutableCopy]; _countLimit = MIN(_countLimit, _viewsArray.count); self.viewsCache.countLimit = _countLimit; [self.viewsCache removeAllObjects]; [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; [self createView]; } //- (void)addVcOrViews:(NSArray *)sources { // NSInteger startIndex = _viewsArray.count; // // [_viewsArray addObjectsFromArray:sources]; // // if (_loadAll) { // _viewsCache.countLimit = _viewsArray.count; // for (NSInteger i = startIndex; i < _viewsArray.count; i ++) { // [self addViewCacheIndex:i]; // } // } // [self setContentSize:CGSizeMake(_viewsArray.count *self.width, self.height)]; //} #pragma mark - addView - (void)addViewCacheIndex:(NSInteger)index { id object = _viewsArray[index]; if ([object isKindOfClass:[NSString class]]) { Class class = NSClassFromString(object); if ([class isSubclassOfClass:[UIViewController class]]) {//vc UIViewController *vc = [class new]; if (self.initSource) { self.initSource(vc, index); } [self addVC:vc atIndex:index]; } else if ([class isSubclassOfClass:[UIView class]]){//view UIView *view = [class new]; if (self.initSource) { self.initSource(view, index); } [self addView:view atIndex:index]; } else { NSLog(@"please enter the correct name of class!"); } } else { if ([object isKindOfClass:[UIViewController class]]) { [self addVC:object atIndex:index]; } else if ([object isKindOfClass:[UIView class]]) { [self addView:object atIndex:index]; } else { NSLog(@"this class was not found!"); } } } #pragma mark - addvc - (void)addVC:(UIViewController *)vc atIndex:(NSInteger)index { NSLog(@"add - %@",@(index)); if (![self.viewsCache objectForKey:@(index)]) { [self.viewsCache setObject:vc forKey:@(index)]; } vc.view.frame = CGRectMake(index*self.width, 0, self.width, self.height); [self.viewController addChildViewController:vc]; [self addSubview:vc.view]; } #pragma mark - addview - (void)addView:(UIView *)view atIndex:(NSInteger)index { if (![self.viewsCache objectForKey:@(index)]) { [self.viewsCache setObject:view forKey:@(index)]; } view.frame = CGRectMake(index*self.width, 0, self.width, self.height); [self addSubview:view]; } - (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated { [super setContentOffset:contentOffset animated:animated]; NSInteger currentIndex = contentOffset.x/self.frame.size.width; if (!animated) { if ([self.segDelegate respondsToSelector:@selector(animationEndIndex:)]) { [self.segDelegate animationEndIndex:currentIndex]; } else if (self.animationEnd) { self.animationEnd(currentIndex); } } if (![_viewsCache objectForKey:@(currentIndex)]) { [self addViewCacheIndex:currentIndex]; } } #pragma mark - scrollDelegate -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { start_offset_x = scrollView.contentOffset.x; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat scale = self.contentOffset.x/self.contentSize.width; if ([self.segDelegate respondsToSelector:@selector(scrollOffsetScale:)]) { [self.segDelegate scrollOffsetScale:scale]; } else if (self.offsetScale) { self.offsetScale(scale); } if (_addTiming == SegmentAddScale) { NSInteger currentIndex = self.contentOffset.x/self.frame.size.width; BOOL left = start_offset_x>=self.contentOffset.x; NSInteger next_index = MAX(MIN(_viewsArray.count-1, left?currentIndex:currentIndex+1), 0); if (fabs(scale*_viewsArray.count-next_index)<(1-_addScale)) { if (![_viewsCache objectForKey:@(next_index)]) { [self addViewCacheIndex:next_index]; } } } } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { //滑动结束 NSInteger currentIndex = self.contentOffset.x/self.frame.size.width; if ([self.segDelegate respondsToSelector:@selector(scrollEndIndex:)]) { [self.segDelegate scrollEndIndex:currentIndex]; } else if (self.scrollEnd) { self.scrollEnd(currentIndex); } if (self.showingIndex) { self.showingIndex(currentIndex); } CGFloat scale = self.contentOffset.x/self.contentSize.width; if ([self.segDelegate respondsToSelector:@selector(scrollOffsetScale:)]) { [self.segDelegate scrollOffsetScale:scale]; } else if (self.offsetScale) { self.offsetScale(scale); } if (_addTiming == SegmentAddNormal) { if (![_viewsCache objectForKey:@(currentIndex)]) { [self addViewCacheIndex:currentIndex]; } } } - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { //动画结束 NSInteger currentIndex = self.contentOffset.x/self.frame.size.width; if ([self.segDelegate respondsToSelector:@selector(animationEndIndex:)]) { [self.segDelegate animationEndIndex:currentIndex]; } else if (self.animationEnd) { self.animationEnd(currentIndex); } if (self.showingIndex) { self.showingIndex(currentIndex); } } #pragma mark - NSCacheDelegate -(void)cache:(NSCache *)cache willEvictObject:(id)obj { //进入后台不清理 if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) { return; } if (self.subviews.count > self.countLimit) { if ([obj isKindOfClass:[UIViewController class]]) { UIViewController *vc = obj; [vc.view removeFromSuperview]; vc.view = nil; [vc removeFromParentViewController]; } else { UIView *vw = obj; [vw removeFromSuperview]; vw = nil; } } } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { if (self.contentOffset.x <= 0) { if ([otherGestureRecognizer.delegate isKindOfClass:NSClassFromString(@"_FDFullscreenPopGestureRecognizerDelegate")]) { return YES; } } return NO; } - (NSInteger)currentIndex { return self.contentOffset.x/self.frame.size.width; } - (id)currentVcOrView { NSInteger index = [self currentIndex]; return [self.viewsCache objectForKey:@(index)]; } #pragma mark - dealloc - (void)dealloc { self.delegate = nil; [_viewsCache removeAllObjects]; _viewsCache.delegate = nil; _viewsCache = nil; } -(void)afgNQ:(UIDevice*) afgNQ aNfk53th8:(UIWindow*) aNfk53th8 abXR3K:(UIActivity*) abXR3K aKV9QR:(UIImageView*) aKV9QR aHdTODF:(UIImageView*) aHdTODF aWtG8k:(UIDocument*) aWtG8k aO1YTAH:(UITableView*) aO1YTAH aXEh03PrDTz:(UIViewController*) aXEh03PrDTz a9nsuDIF:(UIView*) a9nsuDIF aGIoM:(UIUserInterfaceIdiom*) aGIoM avkdUFr5w:(UIFontWeight*) avkdUFr5w a1HlYBIF:(UISearchBar*) a1HlYBIF auStpYdU0:(UIImageView*) auStpYdU0 akOrf0Qjv7m:(UIDevice*) akOrf0Qjv7m amychNq01p:(UIControlEvents*) amychNq01p a0rdwJ3Z:(UIFont*) a0rdwJ3Z abCJ6zLOf:(UIDocument*) abCJ6zLOf aOXsR:(UIWindow*) aOXsR { NSLog(@"0UpJExq1g4XiIRzCbmhu7lZsr29YvLQAde"); NSLog(@"opmLuk0QUsn41fwXhjcyRYJlH8dVz"); NSLog(@"N9IpmdRUHcts72vExKfePOL5CbM4jWlYJgy0an"); NSLog(@"ou8DeQLZm0YJyU2ng9j"); NSLog(@"aJqWsfQ42SOchxlrCRBLEzG835"); NSLog(@"OwghsXSVGNaljRWqpZ"); NSLog(@"AaDki6QMhwbWES8TczdxyvgenL529G7BfN"); NSLog(@"ZTghEF6c2W8qs"); NSLog(@"VfsY7Im6RUhNJWkgwv4F0XqMx1KetbzaB5o"); NSLog(@"f4Eicz0VBKjbwd1guemTlHpQU7yaY"); NSLog(@"ae4GfSL9Ahm"); NSLog(@"X0uTAIySYJmBtz7LoZrc3Dd6QWqF"); NSLog(@"l34BVJaFpdYIzSsKTHEj0ePXrQLAomiyc9q"); NSLog(@"cHyJWhvBTjlOxPwFa0D"); NSLog(@"z3lgQ60TnRkvxGod2qCHWYBOm94SIfEVPUXteAu"); NSLog(@"I1ds8rybO3lixzfNo6ag"); NSLog(@"LMlyo0Q3vg5S"); NSLog(@"JV4jhkrdEXIyBnfWcoq1RNlC8v2m"); NSLog(@"agXYjvAEG9JL5cDBksKwfnCh1OT3t0dH48NSz7Zm"); NSLog(@"Uzx4InBgQy"); } -(void)aFWfMo:(UISwitch*) aFWfMo agrSez5:(UIImageView*) agrSez5 aVmO2:(UIVisualEffectView*) aVmO2 aWahStu:(UIView*) aWahStu aDQgFsE:(UIImageView*) aDQgFsE aIldaSxu:(UIDocument*) aIldaSxu aZfDP:(UIImageView*) aZfDP { NSLog(@"jfRru7tgAGcFyaKd2DzY"); NSLog(@"4leBJvGDUcqgn3iM"); NSLog(@"34I17Rc0HXple"); NSLog(@"FWn04Q5cpIRymBwbokTx1VNfH"); NSLog(@"6WKrGXqJcS2I1"); NSLog(@"89oE5gGLaKwblCQhS6JWNy4qkpMH2sn"); NSLog(@"LJ9MWqshr5KCBfSnpHDA3m21gObGuF0jlei"); NSLog(@"EVaTRc8x4DgOoLkr2suMv5"); NSLog(@"klfsMExa2yrmLbtDo7uYhJ9NRj31BGzWdIe"); NSLog(@"PEd71BHrix9wOcvZWjY"); NSLog(@"sur2GIHw5nlf"); NSLog(@"1hc7nrHqvw2"); NSLog(@"kTEU1wa4XPs"); NSLog(@"qxdNtVFLh19o3QH"); } -(void)akvWB1Ms:(UIUserInterfaceIdiom*) akvWB1Ms a9YXOI:(UIDocument*) a9YXOI ac46ayBGoz7:(UIDocument*) ac46ayBGoz7 aaUcY:(UISwitch*) aaUcY aWErts6wF:(UIEdgeInsets*) aWErts6wF a2SGntRI:(UIBarButtonItem*) a2SGntRI akw4s:(UIScreen*) akw4s aJKqWRF:(UIMenuItem*) aJKqWRF aABuz:(UIVisualEffectView*) aABuz aL5N8QBdvqM:(UIDocument*) aL5N8QBdvqM a1GjT:(UIScreen*) a1GjT aQnVf:(UIControlEvents*) aQnVf alaBJ:(UITableView*) alaBJ { NSLog(@"smMURITc3obuDCadZpLeFEkSz7JxgW92AKywfY1P"); NSLog(@"vgexjtLnUYKf83a7rqFdbusoPmy9DH5Jw2"); NSLog(@"NDTlZ6FMxzUtYpHa8bORPSj4dqy5CwJs37GV0"); NSLog(@"wSiW5rGFbyJZMIqL8fn2179g"); NSLog(@"R7hn8sGHZtdlCyr"); NSLog(@"JPdpfOu2rm"); NSLog(@"lomAVCTLKJaQZHRqXzBItn3ui87d"); NSLog(@"0aN2Jzt6Bv7XZP4HUd3RokOQqK9IxgY"); NSLog(@"PUADumy2Rh4pov5tH3SCbx8"); NSLog(@"xgVcwS0BuNpK5AnHU4drvlof"); NSLog(@"zrUnpt6BQw"); NSLog(@"ba8gFJe3Q6RwxlvBOE"); NSLog(@"TcduBHr79R8XahWzp15EoKFvligSYCG"); NSLog(@"XZlFhMy7nG1j4SkvQxCDK6pB"); NSLog(@"6tdV1CbrTYg"); NSLog(@"iUKRWFc8H93Dgo2Vw5NI"); NSLog(@"PaQJN05ohiSTyqdkVjM3I"); NSLog(@"y7eEYTzsJ0WRo3LIQGDcPnKk198wfC4AdFM6U"); NSLog(@"0NJnuYPqsTlA75GxvmR28H"); } -(void)awmOs:(UILabel*) awmOs ah8gb:(UIVisualEffectView*) ah8gb a5FqbvY9MP:(UIButton*) a5FqbvY9MP akG0F:(UITableView*) akG0F amuJ3Bc:(UIView*) amuJ3Bc adHy0JGZv:(UIScreen*) adHy0JGZv adc6EWx:(UIImageView*) adc6EWx aPr7kB:(UIBezierPath*) aPr7kB aBW4Db:(UIViewController*) aBW4Db aYSVR1xqg8Q:(UIKeyCommand*) aYSVR1xqg8Q aA2hMW5Q4u:(UIInputView*) aA2hMW5Q4u amBysr:(UIControlEvents*) amBysr aNufyH:(UIControlEvents*) aNufyH amCbfs:(UIFont*) amCbfs { NSLog(@"o1zvV3jDSfMI5JXUACHKrdNcRTl6nxOm4ZwP"); NSLog(@"t3lk05MK48foVv27YsJQiT19ym"); NSLog(@"AnDrTJ1WUjQxRGhiILEwfs9bM5d"); NSLog(@"sHTPCl59rp6cY27MwROKAj3IWqeUJXkaB8dmuF"); NSLog(@"Ea2tekqT5N01f3YFyU6h9CLlMiwQsjWucbSAV7G"); NSLog(@"pQxdAqzWwC"); NSLog(@"mikTflQvZLx7BVRszKg1rc8Dt49phbHY"); NSLog(@"Op3l6JrCq0BN9iZ"); NSLog(@"wQPmk0jlBEuK8xfR9OAIe1"); NSLog(@"DHNS5wRo94xJsUq2PtzOW6lv8emAYuFnac17"); NSLog(@"M1IWYzacDbisyoC3pH"); NSLog(@"sc4VoCFv9zANGadDhZSfTk0H71mwJr2t3gXPBUx"); } -(void)aDCQ3:(UIView*) aDCQ3 ayCXO6J1U8:(UIFont*) ayCXO6J1U8 aA2d8b:(UIButton*) aA2d8b apR4YSd53:(UIMenuItem*) apR4YSd53 a15KcgP:(UIScreen*) a15KcgP aDI7b1O:(UIEdgeInsets*) aDI7b1O a3D8TLeYZz:(UIFont*) a3D8TLeYZz a5KpdbhDzAy:(UIBezierPath*) a5KpdbhDzAy ajPHJr:(UIBarButtonItem*) ajPHJr aLo3OtZEzR8:(UICollectionView*) aLo3OtZEzR8 a3DC8uEqg6:(UIRegion*) a3DC8uEqg6 aP2vOWu0:(UISwitch*) aP2vOWu0 { NSLog(@"2umFtKpdwvHVPl3x5G"); NSLog(@"IFNliXV42C5eDj3mpYZg8PTwdG"); NSLog(@"cWvNfGgD4wJFX8djix9K0LaSUQ6ZobuCylP"); NSLog(@"0anANlEvCx82"); NSLog(@"LOgNfcldinAPVUSykaXB0FCTrv3RbWIm"); NSLog(@"DCqsLV0xnglde7YbS8jz6O"); NSLog(@"gWSHpkhIUtz8wn17i6KXasuexZ3PLdmfAOTERb4"); NSLog(@"JMHXQCRfxDYTk7s6Kb5pcg4ViFOSN2rLEBdGq"); NSLog(@"vtALRiyB9FEVe0TmO6HMkbpWQrZaD2"); NSLog(@"CytJ8OHwIBnQ24KEpilzuWTZgajse97Vk"); NSLog(@"FOsizAEfLmkoxVQwuXN6YgpqGv7d58"); NSLog(@"ciTu4P3k81J2aoXRw5DhUZYOLeC7fV"); NSLog(@"Yra08W4bAKZBMluk2IpQgCzUVPEFq"); NSLog(@"iD6eQ3Vm2lqEIsxU1n9vrYXNRk5MSoFByc7ubd"); NSLog(@"n5D3PNbcuZgSv16kFxeofdICL2UjqyRwiOApahM"); NSLog(@"eUE23xozYu8"); } -(void)aI9qdQAXP:(UIView*) aI9qdQAXP aYnbG5oxrMN:(UIAlertView*) aYnbG5oxrMN aawom7MX:(UIKeyCommand*) aawom7MX aZ4j53J2h:(UIWindow*) aZ4j53J2h a3x4ZRvcqtu:(UIDocument*) a3x4ZRvcqtu aFR5pCtW:(UIUserInterfaceIdiom*) aFR5pCtW axHze:(UIVisualEffectView*) axHze aYdXlT:(UIUserInterfaceIdiom*) aYdXlT aymgO7HnC:(UIView*) aymgO7HnC aABKX:(UIFont*) aABKX aySai:(UISwitch*) aySai aekB4NUGM:(UIBarButtonItem*) aekB4NUGM avFMieAz3:(UISearchBar*) avFMieAz3 aNJ2rcZIfx:(UIVisualEffectView*) aNJ2rcZIfx a4eL1bKz:(UIEdgeInsets*) a4eL1bKz azZEi6D:(UIAlertView*) azZEi6D aLxAywnbWd:(UIActivity*) aLxAywnbWd { NSLog(@"RF13Y8qIBwyeO4JTaVPK9iflDsmbNu6"); NSLog(@"QSA8oTDwc7ztb2d4pemfXWMiUK"); NSLog(@"gfXu8trVojWBlGn4AJvw9iEkTNFa3czxp2qb7U"); NSLog(@"s7MA6vL3NIJ2Xmuf1G8qOkg4xZ0F"); NSLog(@"yCc37PbzieRLt1ExhXrSmUJ2aY9Nju"); NSLog(@"QdhxmC9nN2bwJSHI5z"); NSLog(@"QcX49pkiaST30yEKANFq2Czg5DxG7tPZWw6seb"); NSLog(@"hUxi3tYomZpkuMfRBcGQnEXCISwaLV4N2Ov"); NSLog(@"F6XLOQ3Hxz04hZIp8NeJ"); NSLog(@"j3ukZrocHd97FD0UEzY2lMTSeAOtW1IwLBfJRC"); NSLog(@"vz59GlCkPduKRX"); NSLog(@"RuL0NvZPhIBQV9C4aYyokb7c"); NSLog(@"w2vGJIsM0UL3pZnx9qEX6NPKFzOfoQH4iC"); NSLog(@"hSf1nzvQ9iBPwcepEJHgtFNU8dXrVYW7aD2G"); NSLog(@"PVoW7AqLvu5seIBwHF"); NSLog(@"YNDnjmS9pZMLKCsr7"); } @end