省钱达人

MLMSegmentScroll.m 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // MLMSegmentScroll.m
  3. // MLMSegmentPage
  4. //
  5. // Created by my on 2017/2/6.
  6. // Copyright © 2017年 my. All rights reserved.
  7. //
  8. #import "MLMSegmentScroll.h"
  9. #import "SegmentPageHead.h"
  10. @interface MLMSegmentScroll () <NSCacheDelegate,UIScrollViewDelegate>
  11. {
  12. CGFloat start_offset_x;
  13. }
  14. @property (nonatomic, strong) NSCache *viewsCache;//存储页面(使用计数功能)
  15. @property (nonatomic, strong) NSMutableArray *viewsArray;
  16. @end
  17. @implementation MLMSegmentScroll
  18. #pragma mark - init Method
  19. - (instancetype)initWithFrame:(CGRect)frame vcOrViews:(NSArray *)sources {
  20. if (self = [super initWithFrame:frame]) {
  21. _viewsArray = [sources mutableCopy];
  22. [self defaultSet];
  23. }
  24. return self;
  25. }
  26. #pragma mark - default setting
  27. - (void)defaultSet {
  28. WEAK(weakSelf, self)
  29. self.showsVerticalScrollIndicator = NO;
  30. self.showsHorizontalScrollIndicator = NO;
  31. self.pagingEnabled = YES;
  32. self.bounces = NO;
  33. self.delegate = weakSelf;
  34. [self setContentSize:CGSizeMake(_viewsArray.count *self.width, self.height)];
  35. _countLimit = _viewsArray.count;
  36. }
  37. #pragma mark - viewsCache
  38. - (NSCache *)viewsCache {
  39. if (!_viewsCache) {
  40. WEAK(weakSelf, self)
  41. _viewsCache = [[NSCache alloc] init];
  42. _viewsCache.countLimit = _countLimit;
  43. _viewsCache.delegate = weakSelf;
  44. _viewsCache.evictsObjectsWithDiscardedContent = YES;
  45. }
  46. return _viewsCache;
  47. }
  48. #pragma mark - default add View
  49. - (void)createView {
  50. _showIndex = MIN(_viewsArray.count-1, MAX(0, _showIndex));
  51. [self setContentOffset:CGPointMake(_showIndex * self.frame.size.width, 0)];
  52. if (_loadAll) {
  53. NSInteger startIndex;
  54. if (_viewsArray.count-_showIndex > _countLimit) {
  55. startIndex = _showIndex;
  56. } else {
  57. startIndex = _viewsArray.count - _countLimit;
  58. }
  59. for (NSInteger i = startIndex; i < startIndex + _countLimit; i ++) {
  60. [self addViewCacheIndex:i];
  61. }
  62. } else {
  63. [self setContentOffset:CGPointMake(_showIndex*self.width, 0) animated:NO];
  64. }
  65. }
  66. #pragma mark - changeSource
  67. - (void)changeSource:(NSArray *)sources {
  68. _viewsArray = [sources mutableCopy];
  69. _countLimit = MIN(_countLimit, _viewsArray.count);
  70. self.viewsCache.countLimit = _countLimit;
  71. [self.viewsCache removeAllObjects];
  72. [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  73. [self createView];
  74. }
  75. //- (void)addVcOrViews:(NSArray *)sources {
  76. // NSInteger startIndex = _viewsArray.count;
  77. //
  78. // [_viewsArray addObjectsFromArray:sources];
  79. //
  80. // if (_loadAll) {
  81. // _viewsCache.countLimit = _viewsArray.count;
  82. // for (NSInteger i = startIndex; i < _viewsArray.count; i ++) {
  83. // [self addViewCacheIndex:i];
  84. // }
  85. // }
  86. // [self setContentSize:CGSizeMake(_viewsArray.count *self.width, self.height)];
  87. //}
  88. #pragma mark - addView
  89. - (void)addViewCacheIndex:(NSInteger)index {
  90. id object = _viewsArray[index];
  91. if ([object isKindOfClass:[NSString class]]) {
  92. Class class = NSClassFromString(object);
  93. if ([class isSubclassOfClass:[UIViewController class]]) {//vc
  94. UIViewController *vc = [class new];
  95. if (self.initSource) {
  96. self.initSource(vc, index);
  97. }
  98. [self addVC:vc atIndex:index];
  99. } else if ([class isSubclassOfClass:[UIView class]]){//view
  100. UIView *view = [class new];
  101. if (self.initSource) {
  102. self.initSource(view, index);
  103. }
  104. [self addView:view atIndex:index];
  105. } else {
  106. NSLog(@"please enter the correct name of class!");
  107. }
  108. } else {
  109. if ([object isKindOfClass:[UIViewController class]]) {
  110. [self addVC:object atIndex:index];
  111. } else if ([object isKindOfClass:[UIView class]]) {
  112. [self addView:object atIndex:index];
  113. } else {
  114. NSLog(@"this class was not found!");
  115. }
  116. }
  117. }
  118. #pragma mark - addvc
  119. - (void)addVC:(UIViewController *)vc atIndex:(NSInteger)index {
  120. NSLog(@"add - %@",@(index));
  121. if (![self.viewsCache objectForKey:@(index)]) {
  122. [self.viewsCache setObject:vc forKey:@(index)];
  123. }
  124. vc.view.frame = CGRectMake(index*self.width, 0, self.width, self.height);
  125. [self.viewController addChildViewController:vc];
  126. [self addSubview:vc.view];
  127. }
  128. #pragma mark - addview
  129. - (void)addView:(UIView *)view atIndex:(NSInteger)index {
  130. if (![self.viewsCache objectForKey:@(index)]) {
  131. [self.viewsCache setObject:view forKey:@(index)];
  132. }
  133. view.frame = CGRectMake(index*self.width, 0, self.width, self.height);
  134. [self addSubview:view];
  135. }
  136. - (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated {
  137. [super setContentOffset:contentOffset animated:animated];
  138. NSInteger currentIndex = contentOffset.x/self.frame.size.width;
  139. if (!animated) {
  140. if ([self.segDelegate respondsToSelector:@selector(animationEndIndex:)]) {
  141. [self.segDelegate animationEndIndex:currentIndex];
  142. } else if (self.animationEnd) {
  143. self.animationEnd(currentIndex);
  144. }
  145. }
  146. if (![_viewsCache objectForKey:@(currentIndex)]) {
  147. [self addViewCacheIndex:currentIndex];
  148. }
  149. }
  150. #pragma mark - scrollDelegate
  151. -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  152. start_offset_x = scrollView.contentOffset.x;
  153. }
  154. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  155. CGFloat scale = self.contentOffset.x/self.contentSize.width;
  156. if ([self.segDelegate respondsToSelector:@selector(scrollOffsetScale:)]) {
  157. [self.segDelegate scrollOffsetScale:scale];
  158. } else if (self.offsetScale) {
  159. self.offsetScale(scale);
  160. }
  161. if (_addTiming == SegmentAddScale) {
  162. NSInteger currentIndex = self.contentOffset.x/self.frame.size.width;
  163. BOOL left = start_offset_x>=self.contentOffset.x;
  164. NSInteger next_index = MAX(MIN(_viewsArray.count-1, left?currentIndex:currentIndex+1), 0);
  165. if (fabs(scale*_viewsArray.count-next_index)<(1-_addScale)) {
  166. if (![_viewsCache objectForKey:@(next_index)]) {
  167. [self addViewCacheIndex:next_index];
  168. }
  169. }
  170. }
  171. }
  172. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  173. //滑动结束
  174. NSInteger currentIndex = self.contentOffset.x/self.frame.size.width;
  175. if ([self.segDelegate respondsToSelector:@selector(scrollEndIndex:)]) {
  176. [self.segDelegate scrollEndIndex:currentIndex];
  177. } else if (self.scrollEnd) {
  178. self.scrollEnd(currentIndex);
  179. }
  180. if (self.showingIndex) {
  181. self.showingIndex(currentIndex);
  182. }
  183. CGFloat scale = self.contentOffset.x/self.contentSize.width;
  184. if ([self.segDelegate respondsToSelector:@selector(scrollOffsetScale:)]) {
  185. [self.segDelegate scrollOffsetScale:scale];
  186. } else if (self.offsetScale) {
  187. self.offsetScale(scale);
  188. }
  189. if (_addTiming == SegmentAddNormal) {
  190. if (![_viewsCache objectForKey:@(currentIndex)]) {
  191. [self addViewCacheIndex:currentIndex];
  192. }
  193. }
  194. }
  195. - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
  196. //动画结束
  197. NSInteger currentIndex = self.contentOffset.x/self.frame.size.width;
  198. if ([self.segDelegate respondsToSelector:@selector(animationEndIndex:)]) {
  199. [self.segDelegate animationEndIndex:currentIndex];
  200. } else if (self.animationEnd) {
  201. self.animationEnd(currentIndex);
  202. }
  203. if (self.showingIndex) {
  204. self.showingIndex(currentIndex);
  205. }
  206. }
  207. #pragma mark - NSCacheDelegate
  208. -(void)cache:(NSCache *)cache willEvictObject:(id)obj {
  209. //进入后台不清理
  210. if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
  211. return;
  212. }
  213. if (self.subviews.count > self.countLimit) {
  214. if ([obj isKindOfClass:[UIViewController class]]) {
  215. UIViewController *vc = obj;
  216. [vc.view removeFromSuperview];
  217. vc.view = nil;
  218. [vc removeFromParentViewController];
  219. } else {
  220. UIView *vw = obj;
  221. [vw removeFromSuperview];
  222. vw = nil;
  223. }
  224. }
  225. }
  226. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
  227. if (self.contentOffset.x <= 0) {
  228. if ([otherGestureRecognizer.delegate isKindOfClass:NSClassFromString(@"_FDFullscreenPopGestureRecognizerDelegate")]) {
  229. return YES;
  230. }
  231. }
  232. return NO;
  233. }
  234. - (NSInteger)currentIndex {
  235. return self.contentOffset.x/self.frame.size.width;
  236. }
  237. - (id)currentVcOrView {
  238. NSInteger index = [self currentIndex];
  239. return [self.viewsCache objectForKey:@(index)];
  240. }
  241. #pragma mark - dealloc
  242. - (void)dealloc {
  243. self.delegate = nil;
  244. [_viewsCache removeAllObjects];
  245. _viewsCache.delegate = nil;
  246. _viewsCache = nil;
  247. }
  248. @end