Keine Beschreibung

KXMainHorizontalCycleView.m 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // KXMainHorizontalCycleView.m
  3. // QBCS
  4. //
  5. // Created by kuxuan on 2017/6/6.
  6. // Copyright © 2017年 kuxuan. All rights reserved.
  7. //
  8. #import "KXMainHorizontalCycleView.h"
  9. #import "KXCycleModel.h"
  10. #import "KXCustomPageControl.h"
  11. @interface KXMainHorizontalCycleView()<UIScrollViewDelegate>
  12. @property (nonatomic,strong)UIScrollView *topScrollView;
  13. @property (nonatomic,weak)NSTimer *scrollTimer;
  14. @property (nonatomic,strong)KXCustomPageControl *pageControl;
  15. @end
  16. @implementation KXMainHorizontalCycleView
  17. - (id)initWithFrame:(CGRect)frame
  18. {
  19. self = [super initWithFrame:frame];
  20. if (self) {
  21. // Initialization code
  22. [self createView];
  23. }
  24. return self;
  25. }
  26. -(void)createView
  27. {
  28. if (self.topScrollView) {
  29. [self.topScrollView removeFromSuperview];
  30. }
  31. //创建轮播
  32. UIScrollView *sc=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0, self.frame.size.width,120*SCREEN_MUTI)];
  33. self.topScrollView=sc;
  34. self.topScrollView.bounces = NO;
  35. self.topScrollView.pagingEnabled = YES;
  36. self.topScrollView.showsHorizontalScrollIndicator = NO;
  37. self.topScrollView.showsVerticalScrollIndicator = NO;
  38. self.topScrollView.delegate = self;
  39. self.topScrollView.scrollEnabled=YES;
  40. self.topScrollView.tag=6666;
  41. [self addSubview:self.topScrollView];
  42. }
  43. -(void)setupTimer
  44. {
  45. NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(automaticScroll) userInfo:nil repeats:YES];
  46. self.scrollTimer = timer;
  47. [[NSRunLoop mainRunLoop] addTimer:self.scrollTimer forMode:NSRunLoopCommonModes];
  48. }
  49. -(void)automaticScroll
  50. {
  51. NSInteger index=self.topScrollView.contentOffset.x/SCREEN_WIDTH;
  52. index=index+1;
  53. [UIView animateWithDuration:1 animations:^{
  54. self.topScrollView.contentOffset=CGPointMake(SCREEN_WIDTH*index, 0);
  55. }];
  56. self.pageControl.indexNumWithSlide=index;
  57. if (index==self.scrollArray.count+1) {
  58. [self.topScrollView setContentOffset:CGPointMake(CGRectGetWidth(self.topScrollView.frame), 0) animated:NO];
  59. self.pageControl.indexNumWithSlide=0;
  60. }else if(index==0)
  61. {
  62. [self.topScrollView setContentOffset:CGPointMake(CGRectGetWidth(self.topScrollView.frame)*self.scrollArray.count, 0) animated:NO];
  63. self.pageControl.indexNumWithSlide=self.scrollArray.count;
  64. }else{
  65. self.pageControl.indexNumWithSlide=index-1;
  66. }
  67. }
  68. #pragma mark - UIScrollViewDelegate
  69. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  70. {
  71. if (scrollView.tag==6666) {
  72. int pageNum=self.topScrollView.contentOffset.x/SCREEN_WIDTH;
  73. self.pageControl.indexNumWithSlide=pageNum;
  74. if (pageNum==self.scrollArray.count+1) {
  75. [self.topScrollView setContentOffset:CGPointMake(CGRectGetWidth(scrollView.frame), 0) animated:NO];
  76. self.pageControl.indexNumWithSlide=0;
  77. }else if(pageNum==0)
  78. {
  79. [self.topScrollView setContentOffset:CGPointMake(CGRectGetWidth(scrollView.frame)*self.scrollArray.count, 0) animated:NO];
  80. self.pageControl.indexNumWithSlide=self.scrollArray.count;
  81. }
  82. else
  83. {
  84. self.pageControl.indexNumWithSlide=pageNum-1;
  85. }
  86. }
  87. }
  88. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  89. {
  90. [self.scrollTimer invalidate];
  91. self.scrollTimer = nil;
  92. }
  93. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
  94. {
  95. [self setupTimer];
  96. }
  97. - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
  98. {
  99. if (scrollView.tag==6666) {
  100. int pageNum=scrollView.contentOffset.x/CGRectGetWidth(scrollView.frame);
  101. self.pageControl.indexNumWithSlide=pageNum;
  102. if (pageNum==self.scrollArray.count+1) {
  103. [self.topScrollView setContentOffset:CGPointMake(CGRectGetWidth(scrollView.frame), 0) animated:NO];
  104. self.pageControl.indexNumWithSlide=0;
  105. }
  106. else if(pageNum==0)
  107. {
  108. [self.topScrollView setContentOffset:CGPointMake(CGRectGetWidth(scrollView.frame)*self.scrollArray.count, 0) animated:NO];
  109. self.pageControl.indexNumWithSlide=self.scrollArray.count;
  110. }
  111. else
  112. {
  113. self.pageControl.indexNumWithSlide=pageNum-1;
  114. }
  115. }
  116. }
  117. -(void)setScrollArray:(NSArray *)scrollArray
  118. {
  119. _scrollArray=scrollArray;
  120. if (_pageControl) {
  121. [_pageControl removeFromSuperview];
  122. }
  123. _pageControl = [KXCustomPageControl cusPageControlWithView:CGRectMake(SCREEN_WIDTH-100,self.frame.size.height-20, 100, 20) pageNum:scrollArray.count currentPageIndex:0 currentShowImage:[UIImage imageNamed:@"home_pageControl_select"] pageIndicatorShowImage:[UIImage imageNamed:@"home_pageControl_normal"]];
  124. [self addSubview:_pageControl];
  125. for (int i=0; i<self.scrollArray.count+2; i++) {
  126. UIImageView *imageView=[[UIImageView alloc]init];
  127. imageView.frame=CGRectMake(i*self.frame.size.width, 0, self.frame.size.width, 120*SCREEN_MUTI);
  128. imageView.userInteractionEnabled=YES;
  129. if (i==0) {
  130. KXCycleModel *model=scrollArray[scrollArray.count-1];
  131. [imageView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"placeholder_rectangle"] options:SDWebImageRefreshCached];
  132. }else if (i==self.scrollArray.count+1){
  133. KXCycleModel *model=scrollArray[0];
  134. [imageView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"placeholder_rectangle"] options:SDWebImageRefreshCached];
  135. }else{
  136. KXCycleModel *model=scrollArray[i-1];
  137. [imageView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"placeholder_rectangle"] options:SDWebImageRefreshCached];
  138. imageView.tag=2345+i;
  139. }
  140. UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
  141. [imageView addGestureRecognizer:tap];
  142. [self.topScrollView addSubview:imageView];
  143. }
  144. self.topScrollView.contentSize=CGSizeMake((self.scrollArray.count+2)*SCREEN_WIDTH, 0);
  145. self.topScrollView.contentOffset=CGPointMake(SCREEN_WIDTH, 0);
  146. // self.pageControl.pageIndicatorTintColor=[UIColor colorWithWhite:1 alpha:0.4];
  147. // self.pageControl.currentPageIndicatorTintColor=[UIColor whiteColor];
  148. [self setupTimer];
  149. }
  150. //- (UIPageControl *)pageControl{
  151. // if (!_pageControl) {
  152. // _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-100,self.frame.size.height-20, 100, 20)];
  153. //// [_pageControl setValue:[UIImage imageNamed:@"home_pageControl_normal"] forKey:@"_pageImage"];
  154. //// [_pageControl setValue:[UIImage imageNamed:@"home_pageControl_select"] forKey:@"_currentPageImage"];
  155. // _pageControl.pageIndicatorTintColor=[UIColor colorWithWhite:1 alpha:0.4];
  156. // _pageControl.currentPageIndicatorTintColor=[UIColor whiteColor];
  157. // }
  158. // return _pageControl;
  159. //}
  160. -(void)dealloc
  161. {
  162. self.topScrollView.delegate=nil;
  163. }
  164. -(void)tapAction:(UITapGestureRecognizer *)tap
  165. {
  166. [_delegate clickScrollViewItemWithIndex:tap.view.tag-2346];
  167. }
  168. @end