Nessuna descrizione

KXMainVerticleCycleView.m 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // KXMainVerticleCycleView.m
  3. // CAISHEN
  4. //
  5. // Created by kuxuan on 2017/8/24.
  6. // Copyright © 2017年 kuxuan. All rights reserved.
  7. //
  8. #import "KXMainVerticleCycleView.h"
  9. @interface KXMainVerticleCycleView ()<UIScrollViewDelegate>
  10. {
  11. UIImageView *imageView;
  12. UIView *lineView;
  13. }
  14. @property (nonatomic,weak)UIScrollView *scrollView;
  15. @property (nonatomic,weak)NSTimer *scrollTimer;
  16. @end
  17. @implementation KXMainVerticleCycleView
  18. - (id)initWithFrame:(CGRect)frame
  19. {
  20. self = [super initWithFrame:frame];
  21. if (self) {
  22. // Initialization code
  23. [self createView];
  24. }
  25. return self;
  26. }
  27. -(void)createView
  28. {
  29. if (self.scrollView) {
  30. [self.scrollView removeFromSuperview];
  31. }
  32. imageView=[[UIImageView alloc]initWithFrame:CGRectMake(Fitsize(9),13*SCREEN_MUTI,36*SCREEN_MUTI, 25*SCREEN_MUTI)];
  33. imageView.userInteractionEnabled=YES;
  34. imageView.image=[UIImage imageNamed:@"main_notice"];
  35. [self addSubview:imageView];
  36. //创建轮播
  37. UIScrollView *sc=[[UIScrollView alloc]initWithFrame:CGRectMake(Fitsize(53), 0, SCREEN_WIDTH-42, SCREEN_MUTI*30)];
  38. self.scrollView=sc;
  39. self.scrollView.bounces = NO;
  40. self.scrollView.pagingEnabled = YES;
  41. self.scrollView.showsHorizontalScrollIndicator = NO;
  42. self.scrollView.showsVerticalScrollIndicator = NO;
  43. self.scrollView.alwaysBounceVertical=YES;
  44. self.scrollView.alwaysBounceHorizontal=NO;
  45. self.scrollView.delegate = self;
  46. self.scrollView.scrollEnabled=YES;
  47. self.scrollView.tag=6667;
  48. [self addSubview:self.scrollView];
  49. }
  50. -(void)setImageName:(NSString *)imageName {
  51. _imageName = imageName;
  52. imageView.image=[UIImage imageNamed:imageName];
  53. }
  54. -(void)setupTimer
  55. {
  56. NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(automaticScroll) userInfo:nil repeats:YES];
  57. self.scrollTimer = timer;
  58. [[NSRunLoop mainRunLoop] addTimer:self.scrollTimer forMode:NSRunLoopCommonModes];
  59. }
  60. -(void)automaticScroll
  61. {
  62. NSInteger index=self.scrollView.contentOffset.y/(30*SCREEN_MUTI)+0.5;
  63. index=index+1;
  64. [UIView animateWithDuration:1 animations:^{
  65. self.scrollView.contentOffset=CGPointMake(0, 30*SCREEN_MUTI*index);
  66. }];
  67. if (index==self.noticeArray.count+1) {
  68. [self.scrollView setContentOffset:CGPointMake(0, CGRectGetHeight(self.scrollView.frame)) animated:NO];
  69. }else if(index==0)
  70. {
  71. [self.scrollView setContentOffset:CGPointMake(0, CGRectGetHeight(self.scrollView.frame)*self.noticeArray.count) animated:NO];
  72. }
  73. }
  74. #pragma mark - UIScrollViewDelegate
  75. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  76. {
  77. if (scrollView.tag==6667) {
  78. int pageNum=self.scrollView.contentOffset.y/self.frame.size.height;
  79. if (pageNum==self.noticeArray.count+1) {
  80. [self.scrollView setContentOffset:CGPointMake(0, CGRectGetHeight(scrollView.frame)) animated:NO];
  81. }else if(pageNum==0)
  82. {
  83. [self.scrollView setContentOffset:CGPointMake(0, CGRectGetHeight(self.scrollView.frame)*self.noticeArray.count) animated:NO];
  84. }
  85. }
  86. }
  87. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  88. {
  89. [self.scrollTimer invalidate];
  90. self.scrollTimer = nil;
  91. }
  92. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
  93. {
  94. [self setupTimer];
  95. }
  96. - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
  97. {
  98. if (scrollView.tag==6667) {
  99. int pageNum=scrollView.contentOffset.y/CGRectGetHeight(scrollView.frame);
  100. if (pageNum==self.noticeArray.count+1) {
  101. [self.scrollView setContentOffset:CGPointMake(0, CGRectGetHeight(scrollView.frame)) animated:NO];
  102. }
  103. else if(pageNum==0)
  104. {
  105. [self.scrollView setContentOffset:CGPointMake(0, CGRectGetHeight(self.scrollView.frame)*self.noticeArray.count) animated:NO];
  106. }
  107. }
  108. }
  109. -(void)setNoticeArray:(NSArray *)noticeArray
  110. {
  111. _noticeArray=noticeArray;
  112. if (noticeArray.count == 0){
  113. return;
  114. }
  115. for (int i=0; i<self.noticeArray.count+2; i++) {
  116. UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,30*SCREEN_MUTI*i, SCREEN_WIDTH-42, 46*SCREEN_MUTI)];
  117. titleLabel.textColor = [UIColor KXColorWithHex:0x666666];
  118. titleLabel.tag=i+2345;
  119. titleLabel.font = FONT_SYS(14);
  120. [self.scrollView addSubview:titleLabel];
  121. if (i==0) {
  122. NSString *aString=noticeArray[self.noticeArray.count-1];
  123. NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[aString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSFontAttributeName:FONT_SYS(20*SCREEN_MUTI)} documentAttributes:nil error:nil];
  124. [attrStr addAttribute:NSFontAttributeName value:FONT_SYS(14) range:NSMakeRange(0, [attrStr length]-1)];
  125. titleLabel.attributedText = attrStr;
  126. }else if (i==self.noticeArray.count+1){
  127. NSString *aString=noticeArray[0];
  128. NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[aString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSFontAttributeName:FONT_SYS(20*SCREEN_MUTI)} documentAttributes:nil error:nil];
  129. [attrStr addAttribute:NSFontAttributeName value:FONT_SYS(14) range:NSMakeRange(0, [attrStr length]-1)];
  130. titleLabel.attributedText = attrStr;
  131. }else{
  132. NSString *aString=noticeArray[i-1];
  133. NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[aString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSFontAttributeName:FONT_SYS(20*SCREEN_MUTI)} documentAttributes:nil error:nil];
  134. [attrStr addAttribute:NSFontAttributeName value:FONT_SYS(14) range:NSMakeRange(0, [attrStr length]-1)];
  135. titleLabel.attributedText = attrStr;
  136. }
  137. titleLabel.userInteractionEnabled=YES;
  138. UITapGestureRecognizer *titleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(titleTapAction:)];
  139. [titleLabel addGestureRecognizer:titleTap];
  140. }
  141. self.scrollView.contentSize=CGSizeMake(SCREEN_WIDTH-42, (noticeArray.count+2)*(30*SCREEN_MUTI));
  142. self.scrollView.contentOffset=CGPointMake(0, 30.0*SCREEN_MUTI);
  143. if (noticeArray.count!=1) {
  144. [self setupTimer];
  145. }
  146. }
  147. -(void)dealloc
  148. {
  149. self.scrollView.delegate=nil;
  150. }
  151. -(void)titleTapAction:(UITapGestureRecognizer *)tap
  152. {
  153. [_delegate clickmiddleViewWithIndex:tap.view.tag-2346];
  154. }
  155. -(void)setIsHuaJiangHu:(BOOL)isHuaJiangHu {
  156. _isHuaJiangHu = isHuaJiangHu;
  157. if (isHuaJiangHu) {
  158. lineView = [[UIView alloc] init];
  159. [self addSubview:lineView];
  160. [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
  161. make.top.mas_equalTo(imageView.mas_top).mas_offset(5);
  162. make.bottom.mas_equalTo(imageView.mas_bottom).mas_offset(-8);
  163. make.left.mas_equalTo(imageView.mas_right).mas_offset(Fitsize(5));
  164. make.width.mas_equalTo(Fitsize(1));
  165. }];
  166. lineView.backgroundColor = [UIColor KXColorWithHex:0xFF5D49];
  167. }else {
  168. [lineView removeFromSuperview];
  169. lineView = nil;
  170. }
  171. }
  172. @end