Nessuna descrizione

KXTeachCycleView.m 5.6KB

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