ZLImageScrollView.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. //
  2. // ZLImageScrollView.m
  3. // ZLImageScrollViewDemo
  4. //
  5. // Created by Mr.LuDashi on 15/8/20.
  6. // Copyright (c) 2015年 zeluli. All rights reserved.
  7. //
  8. #import "ZLImageScrollView.h"
  9. @interface ZLImageScrollView()<UIScrollViewDelegate>
  10. @property (nonatomic, strong) UIScrollView *mainScrollView;
  11. @property (nonatomic, strong) UIPageControl *mainPageControl;
  12. @property (nonatomic, assign) CGFloat widthOfView;
  13. @property (nonatomic, assign) CGFloat heightView;
  14. @property (nonatomic, strong) NSArray *imagesNameArray;
  15. @property (nonatomic, strong) NSMutableArray *imageViewsArray;
  16. @property (nonatomic, assign) NSInteger currentPage;
  17. @property (nonatomic, strong) NSTimer *timer;
  18. @property (nonatomic, assign) UIViewContentMode imageViewcontentModel;
  19. @property (nonatomic, strong) UIPageControl *imageViewPageControl;
  20. @property (nonatomic, strong) TapImageViewButtonBlock block;
  21. @property (nonatomic, assign) BOOL isRight;
  22. @end
  23. @implementation ZLImageScrollView
  24. #pragma -- 遍历构造器
  25. + (instancetype) zlImageScrollViewWithFrame: (CGRect) frame
  26. WithImages: (NSArray *) images{
  27. ZLImageScrollView *instance = [[ZLImageScrollView alloc] initWithFrame:frame WithImages:images];
  28. return instance;
  29. }
  30. #pragma -- mark 遍历初始化方法
  31. - (instancetype)initWithFrame: (CGRect)frame
  32. WithImages: (NSArray *) images
  33. {
  34. self = [super initWithFrame:frame];
  35. if (self) {
  36. //获取滚动视图的宽度
  37. _widthOfView = frame.size.width;
  38. //获取滚动视图的高度
  39. _heightView = frame.size.height;
  40. _scrollInterval = 3;
  41. _animationInterVale = 0.7;
  42. _isRight = YES;
  43. //当前显示页面
  44. _currentPage = 0;
  45. _imageViewcontentModel = UIViewContentModeScaleAspectFill;
  46. self.clipsToBounds = YES;
  47. _imagesNameArray = images;
  48. //初始化滚动视图
  49. [self initMainScrollView];
  50. //添加ImageView
  51. [self addImageviewsForMainScrollWithImageView];
  52. //添加timer
  53. [self addTimerLoop];
  54. [self addPageControl];
  55. }
  56. return self;
  57. }
  58. #pragma 添加PageControl
  59. - (void) addPageControl{
  60. _imageViewPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, _heightView - 20, _widthOfView, 20)];
  61. _imageViewPageControl.numberOfPages = _imagesNameArray.count;
  62. _imageViewPageControl.currentPage = _currentPage - 1;
  63. _imageViewPageControl.currentPageIndicatorTintColor = [UIColor blackColor];
  64. _imageViewPageControl.pageIndicatorTintColor = [UIColor whiteColor];
  65. [self addSubview:_imageViewPageControl];
  66. }
  67. - (void) addTapEventForImageWithBlock: (TapImageViewButtonBlock) block{
  68. if (_block == nil) {
  69. if (block != nil) {
  70. _block = block;
  71. [self initImageViewButton];
  72. }
  73. }
  74. }
  75. #pragma -- mark 初始化按钮
  76. - (void) initImageViewButton{
  77. for ( int i = 0; i < _imageViewsArray.count + 1; i ++) {
  78. CGRect currentFrame = CGRectMake(_widthOfView * i, 0, _widthOfView, _heightView);
  79. UIButton *tempButton = [[UIButton alloc] initWithFrame:currentFrame];
  80. [tempButton addTarget:self action:@selector(tapImageButton:) forControlEvents:UIControlEventTouchUpInside];
  81. if (i == 0) {
  82. tempButton.tag = _imageViewsArray.count;
  83. } else {
  84. tempButton.tag = i;
  85. }
  86. [_mainScrollView addSubview:tempButton];
  87. }
  88. }
  89. - (void) tapImageButton: (UIButton *) sender{
  90. if (_block) {
  91. _block(_currentPage + 1);
  92. }
  93. }
  94. #pragma -- mark 初始化ScrollView
  95. - (void) initMainScrollView{
  96. _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, _widthOfView, _heightView)];
  97. _mainScrollView.contentSize = CGSizeMake(_widthOfView, _heightView);
  98. _mainScrollView.pagingEnabled = YES;
  99. _mainScrollView.showsHorizontalScrollIndicator = NO;
  100. _mainScrollView.showsVerticalScrollIndicator = NO;
  101. _mainScrollView.delegate = self;
  102. [self addSubview:_mainScrollView];
  103. }
  104. #pragma -- mark 给ScrollView添加ImageView 3个ImageView
  105. -(void) addImageviewsForMainScrollWithImageView{
  106. //设置ContentSize
  107. _mainScrollView.contentSize = CGSizeMake(_widthOfView * 2, _heightView);
  108. _imageViewsArray = [[NSMutableArray alloc] initWithCapacity:2];
  109. for ( int i = 0; i < 2; i ++) {
  110. CGRect currentFrame = CGRectMake(_widthOfView * i, 0, _widthOfView, _heightView);
  111. UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:currentFrame];
  112. tempImageView.contentMode = _imageViewcontentModel;
  113. tempImageView.clipsToBounds = YES;
  114. [_mainScrollView addSubview:tempImageView];
  115. [_imageViewsArray addObject:tempImageView];
  116. }
  117. UIImageView *tempImageView = _imageViewsArray[0];
  118. [tempImageView setImage:[UIImage imageNamed:_imagesNameArray[0]]];
  119. }
  120. - (void) addTimerLoop{
  121. self.translatesAutoresizingMaskIntoConstraints = NO;
  122. if (_timer == nil) {
  123. _timer = [NSTimer scheduledTimerWithTimeInterval:_scrollInterval target:self selector:@selector(changeOffset) userInfo:nil repeats:YES];
  124. }
  125. }
  126. -(void) changeOffset{
  127. //获取ScrollView的offset.x
  128. _currentPage ++;
  129. //如果是最后一个图片,让其成为第一个
  130. if (_currentPage >= _imagesNameArray.count) {
  131. _currentPage = 0;
  132. }
  133. //将要显示的视图
  134. if(_currentPage < _imagesNameArray.count){
  135. UIImageView *tempImageView = _imageViewsArray[1] ;
  136. [tempImageView setImage:[UIImage imageNamed:_imagesNameArray[_currentPage]]];
  137. }
  138. [UIView animateWithDuration:_animationInterVale animations:^{
  139. if(_isRight){
  140. _mainScrollView.contentOffset = CGPointMake(_widthOfView, 0);
  141. } else {
  142. _mainScrollView.contentOffset = CGPointMake(-_widthOfView, 0);
  143. }
  144. } completion:^(BOOL finished) {
  145. //说明是用的第二个ImageView
  146. if (_currentPage < _imagesNameArray.count) {
  147. _mainScrollView.contentOffset = CGPointMake(0, 0);
  148. UIImageView *tempImageView = _imageViewsArray[0] ;
  149. [tempImageView setImage:[UIImage imageNamed:_imagesNameArray[_currentPage]]];
  150. }
  151. }];
  152. _imageViewPageControl.currentPage = _currentPage;
  153. }
  154. -(void) scrollViewDidScroll:(UIScrollView *)scrollView{
  155. CGFloat offsetx = scrollView.contentOffset.x - 0;
  156. if (offsetx > 3) {
  157. [self LoopRightWithBool:YES];
  158. return;
  159. }
  160. if (offsetx < -3) {
  161. [self LoopRightWithBool:NO];
  162. return;
  163. }
  164. }
  165. -(void) scrollViewWillBeginDragging:(UIScrollView *)scrollView{
  166. _currentPage ++;
  167. //如果是最后一个图片,让其成为第一个
  168. if (_currentPage >= _imagesNameArray.count) {
  169. _currentPage = 0;
  170. }
  171. //将要显示的视图
  172. if(_currentPage < _imagesNameArray.count){
  173. UIImageView *tempImageView = _imageViewsArray[1] ;
  174. [tempImageView setImage:[UIImage imageNamed:_imagesNameArray[_currentPage]]];
  175. }
  176. }
  177. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  178. //说明是用的第二个ImageView
  179. if (_currentPage < _imagesNameArray.count) {
  180. _mainScrollView.contentOffset = CGPointMake(0, 0);
  181. UIImageView *tempImageView = _imageViewsArray[0] ;
  182. [tempImageView setImage:[UIImage imageNamed:_imagesNameArray[_currentPage]]];
  183. }
  184. _imageViewPageControl.currentPage = _currentPage;
  185. [self resumeTimer];
  186. }
  187. #pragma 暂停定时器
  188. -(void)resumeTimer{
  189. if (![_timer isValid]) {
  190. return ;
  191. }
  192. [_timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:_scrollInterval-_animationInterVale]];
  193. }
  194. #pragma 改变方向
  195. - (void) LoopRightWithBool: (BOOL) isRight{
  196. _isRight =isRight;
  197. UIImageView *secondImageView = _imageViewsArray[1];
  198. if (isRight) {
  199. secondImageView.frame = CGRectMake(_widthOfView, 0, _widthOfView, _heightView);
  200. } else {
  201. secondImageView.frame = CGRectMake(-_widthOfView, 0, _widthOfView, _heightView);
  202. }
  203. }
  204. @end