新UI马甲包

FSLoopScrollView.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. //
  2. // FSLoopScrollView.m
  3. // SFLoopScrollview
  4. //
  5. // Created by huim on 2017/3/6.
  6. // Copyright © 2017年 shunFSKi. All rights reserved.
  7. //
  8. #import "FSLoopScrollView.h"
  9. #import "NSTimer+FSLoop.h"
  10. #import <UIImageView+WebCache.h>
  11. #import <YYText.h>
  12. @interface FSLoopScrollView ()<UIScrollViewDelegate>
  13. @property (nonatomic, strong)UIPageControl *pageControl;
  14. @property (nonatomic, strong) UIScrollView *scrollView;
  15. @property (nonatomic, strong) UIImageView *lastImgView;
  16. @property (nonatomic, strong) UIImageView *nextImgView;
  17. @property (nonatomic, strong) UIImageView *currentImgView;
  18. @property (nonatomic, strong) YYLabel *currentLabel;
  19. @property (nonatomic, strong) YYLabel *nextLabel;
  20. @property (nonatomic, strong) NSTimer *timer;
  21. @property (nonatomic, assign) BOOL isHorizontal;
  22. @property (nonatomic, assign) BOOL isTitleView;
  23. @end
  24. @implementation FSLoopScrollView
  25. /*
  26. // Only override drawRect: if you perform custom drawing.
  27. // An empty implementation adversely affects performance during animation.
  28. - (void)drawRect:(CGRect)rect {
  29. // Drawing code
  30. }
  31. */
  32. - (void)layoutSubviews
  33. {
  34. [super layoutSubviews];
  35. _lastImgView.frame = CGRectMake(0, 0, CGRectGetWidth(self.scrollView.bounds), CGRectGetHeight(self.scrollView.bounds));
  36. _currentImgView.frame = _isHorizontal?CGRectOffset(_lastImgView.frame, CGRectGetWidth(_lastImgView.bounds), 0):CGRectOffset(_lastImgView.frame, 0, CGRectGetHeight(_lastImgView.bounds));
  37. _nextImgView.frame = _isHorizontal?CGRectOffset(_currentImgView.frame, CGRectGetWidth(_currentImgView.bounds), 0):CGRectOffset(_currentImgView.frame, 0, CGRectGetHeight(_currentImgView.bounds));
  38. _currentLabel.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
  39. _nextLabel.frame = CGRectOffset(_currentLabel.frame, 0, CGRectGetHeight(_currentLabel.bounds));
  40. }
  41. - (instancetype)initWithFrame:(CGRect)frame
  42. {
  43. self = [super initWithFrame:frame];
  44. if (self) {
  45. _timeinterval = 2.0f;
  46. _textColor = [UIColor blackColor];
  47. _textFont = [UIFont systemFontOfSize:15.0f];
  48. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
  49. [self.scrollView addGestureRecognizer:tap];
  50. }
  51. return self;
  52. }
  53. + (instancetype)loopImageViewWithFrame:(CGRect)frame isHorizontal:(BOOL)isHorizontal
  54. {
  55. FSLoopScrollView *loopView = [[self alloc] initWithFrame:frame];
  56. loopView.lastImgView = [[UIImageView alloc]init];
  57. [loopView.scrollView addSubview:loopView.lastImgView];
  58. loopView.nextImgView = [[UIImageView alloc]init];
  59. [loopView.scrollView addSubview:loopView.nextImgView];
  60. loopView.currentImgView = [[UIImageView alloc]init];
  61. [loopView.scrollView addSubview:loopView.currentImgView];
  62. loopView.isHorizontal = isHorizontal;
  63. return loopView;
  64. }
  65. + (instancetype)loopTitleViewWithFrame:(CGRect)frame isTitleView:(BOOL)isTitleView titleImgArr:(NSArray *)titleImgArr
  66. {
  67. FSLoopScrollView *loopView = [[self alloc] initWithFrame:frame];
  68. loopView.currentLabel = [[YYLabel alloc]init];
  69. [loopView.scrollView addSubview:loopView.currentLabel];
  70. loopView.nextLabel = [[YYLabel alloc]init];
  71. [loopView.scrollView addSubview:loopView.nextLabel];
  72. loopView.isTitleView = isTitleView;
  73. loopView.titleImgArr = titleImgArr;
  74. return loopView;
  75. }
  76. #pragma mark Action
  77. - (void)onTimer:(NSTimer *)timer
  78. {
  79. CGPoint pointMake = _isHorizontal?CGPointMake(CGRectGetWidth(self.bounds)*2, 0):CGPointMake(0, CGRectGetHeight(self.bounds)*2);
  80. if (_isTitleView) {
  81. pointMake = CGPointMake(0, CGRectGetHeight(self.bounds));
  82. }
  83. [self.scrollView setContentOffset:pointMake animated:YES];
  84. }
  85. //scrollview的触摸手势
  86. - (void)tapClick:(UITapGestureRecognizer *)tap
  87. {
  88. if (self.tapClickBlock) {
  89. self.tapClickBlock(self);
  90. }
  91. }
  92. //根据index动态更新图片,通过偏移量控制currentImg一直显示在当前位置
  93. - (void)refreshCurrentImageView
  94. {
  95. NSInteger index = _currentIndex;
  96. [self dynamicLoadImageView:_currentImgView imageData:self.imgResourceArr[index]];
  97. index = _currentIndex-1<0?self.imgResourceArr.count-1:_currentIndex-1;
  98. [self dynamicLoadImageView:_lastImgView imageData:self.imgResourceArr[index]];
  99. index = _currentIndex+1>=self.imgResourceArr.count?0:_currentIndex+1;
  100. [self dynamicLoadImageView:_nextImgView imageData:self.imgResourceArr[index]];
  101. CGPoint pointMake = _isHorizontal?CGPointMake(CGRectGetWidth(self.bounds), 0):CGPointMake(0, CGRectGetHeight(self.bounds));
  102. [self.scrollView setContentOffset:pointMake];
  103. }
  104. //更新index
  105. - (void)refreshCurrentIndex
  106. {
  107. if (_isHorizontal) {
  108. if (self.scrollView.contentOffset.x >= CGRectGetWidth(self.bounds)*1.5) {
  109. _currentIndex ++;
  110. if (_currentIndex > self.imgResourceArr.count-1) {
  111. _currentIndex = 0;
  112. }
  113. }else if (self.scrollView.contentOffset.x < CGRectGetWidth(self.bounds)*0.5){
  114. _currentIndex --;
  115. if (_currentIndex < 0) {
  116. _currentIndex = self.imgResourceArr.count-1;
  117. }
  118. }
  119. }else{
  120. if (self.scrollView.contentOffset.y >= CGRectGetHeight(self.bounds)*1.5) {
  121. _currentIndex ++;
  122. if (_currentIndex > self.imgResourceArr.count-1) {
  123. _currentIndex = 0;
  124. }
  125. }else if (self.scrollView.contentOffset.y < CGRectGetHeight(self.bounds)*0.5){
  126. _currentIndex --;
  127. if (_currentIndex < 0) {
  128. _currentIndex = self.imgResourceArr.count-1;
  129. }
  130. }
  131. }
  132. }
  133. //加载图片,网络或本地图片
  134. - (void)dynamicLoadImageView:(UIImageView *)imageView imageData:(id)data
  135. {
  136. if ([data isKindOfClass:[UIImage class]]) {
  137. imageView.image = (UIImage *)data;
  138. }
  139. else if ([data isKindOfClass:[NSString class]]){
  140. NSString *imageName = (NSString *)data;
  141. if ([imageName hasPrefix:@"http"]) {
  142. [imageView sd_setImageWithURL:[NSURL URLWithString:imageName] placeholderImage:nil];
  143. }else{
  144. imageView.image = [UIImage imageNamed:imageName];
  145. }
  146. }
  147. }
  148. - (void)refreshCurrentTitleView
  149. {
  150. NSInteger index = _currentIndex;
  151. self.currentLabel.attributedText = [self getAttachmentTextWithStr:self.titlesArr[index] image:self.titleImgArr[index]];
  152. index = _currentIndex+1>=self.titlesArr.count?0:_currentIndex+1;
  153. self.nextLabel.attributedText = [self getAttachmentTextWithStr:self.titlesArr[index] image:self.titleImgArr[index]];
  154. self.scrollView.contentOffset = CGPointMake(0, 0);
  155. }
  156. - (void)refreshTitleIndex
  157. {
  158. if (self.scrollView.contentOffset.y >= CGRectGetHeight(self.bounds)*0.5) {
  159. _currentIndex ++;
  160. if (_currentIndex > self.titlesArr.count-1) {
  161. _currentIndex = 0;
  162. }
  163. }
  164. }
  165. #pragma mark ----Lazy load
  166. - (UIScrollView *)scrollView
  167. {
  168. if (!_scrollView) {
  169. _scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
  170. _scrollView.delegate = self;
  171. _scrollView.bounces = NO;
  172. _scrollView.pagingEnabled = YES;
  173. _scrollView.scrollsToTop = NO;
  174. _scrollView.showsVerticalScrollIndicator = NO;
  175. _scrollView.showsHorizontalScrollIndicator = NO;
  176. [self addSubview:_scrollView];
  177. }
  178. return _scrollView;
  179. }
  180. - (UIPageControl *)pageControl
  181. {
  182. if (!_pageControl) {
  183. _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds)-20, CGRectGetWidth(self.bounds), 10)];
  184. _pageControl.pageIndicatorTintColor = [UIColor grayColor];
  185. _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
  186. _pageControl.hidesForSinglePage = YES;
  187. [self addSubview:_pageControl];
  188. }
  189. return _pageControl;
  190. }
  191. #pragma mark setter
  192. -(void)setImgResourceArr:(NSArray *)imgResourceArr
  193. {
  194. _imgResourceArr = imgResourceArr;
  195. _currentIndex = 0;
  196. self.pageControl.numberOfPages = self.imgResourceArr.count;
  197. self.pageControl.currentPage = 0;
  198. if (![_timer isValid]) {
  199. _timer = [NSTimer scheduledTimerWithTimeInterval:_timeinterval target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
  200. [_timer pauseTimer];
  201. }
  202. if (imgResourceArr.count<=1) {
  203. self.scrollView.contentSize = _isHorizontal?CGSizeMake(CGRectGetWidth(self.bounds), 0):CGSizeMake(0, CGRectGetHeight(self.bounds));
  204. }else{
  205. self.scrollView.contentSize = _isHorizontal?CGSizeMake(CGRectGetWidth(self.bounds)*3, 0):CGSizeMake(0, CGRectGetHeight(self.bounds)*3);
  206. [self.timer resumeTimerAfterTimeInterval:_timeinterval];
  207. }
  208. [self refreshCurrentImageView];
  209. }
  210. - (void)setTitlesArr:(NSArray *)titlesArr
  211. {
  212. _titlesArr = titlesArr;
  213. _currentIndex = 0;
  214. self.scrollView.scrollEnabled = NO;
  215. if (![_timer isValid]) {
  216. _timer = [NSTimer scheduledTimerWithTimeInterval:_timeinterval target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
  217. [_timer pauseTimer];
  218. }
  219. if (titlesArr.count<=1) {
  220. self.scrollView.contentSize = CGSizeMake(0, CGRectGetHeight(self.bounds));
  221. }else{
  222. self.scrollView.contentSize = CGSizeMake(0, CGRectGetHeight(self.bounds)*2);
  223. [self.timer resumeTimerAfterTimeInterval:_timeinterval];
  224. }
  225. [self refreshCurrentTitleView];
  226. }
  227. - (void)setTitleImgArr:(NSArray *)titleImgArr
  228. {
  229. _titleImgArr = titleImgArr;
  230. }
  231. - (void)setTextFont:(UIFont *)textFont
  232. {
  233. _textFont = textFont;
  234. }
  235. - (void)setTextColor:(UIColor *)textColor
  236. {
  237. _textColor = textColor;
  238. }
  239. #pragma mark UIScrollViewDelegate
  240. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  241. {
  242. //触摸滑动时暂停定时器
  243. [self.timer pauseTimer];
  244. }
  245. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  246. {
  247. _isTitleView?[self refreshTitleIndex]:[self refreshCurrentIndex];
  248. if (self.pageControl.currentPage != _currentIndex) {
  249. self.pageControl.currentPage = _currentIndex;
  250. [self refreshCurrentImageView];
  251. }
  252. //触摸滚动结束重启定时器
  253. [self.timer resumeTimerAfterTimeInterval:_timeinterval];
  254. }
  255. - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
  256. {
  257. _isTitleView?[self refreshTitleIndex]:[self refreshCurrentIndex];
  258. if (_isTitleView) {
  259. [self refreshCurrentTitleView];
  260. }else{
  261. if (self.pageControl.currentPage != _currentIndex) {
  262. self.pageControl.currentPage = _currentIndex;
  263. [self refreshCurrentImageView];
  264. }
  265. }
  266. }
  267. #pragma mark private
  268. /**
  269. 带图片头的文字循环
  270. @param labelText label赋值
  271. @param data 本地图片资源(可置空)
  272. @return 拼接好的富文本
  273. */
  274. - (NSMutableAttributedString *)getAttachmentTextWithStr:(NSString *)labelText image:(id)data
  275. {
  276. UIImage *image;
  277. if ([data isKindOfClass:[UIImage class]]) {
  278. image = (UIImage *)data;
  279. }else if ([data isKindOfClass:[NSString class]]){
  280. NSString *imageName = (NSString *)data;
  281. image = [UIImage imageNamed:imageName];
  282. }
  283. NSMutableAttributedString *text = [NSMutableAttributedString new];
  284. NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:self.textFont alignment:YYTextVerticalAlignmentCenter];
  285. [text appendAttributedString:attachText];
  286. [text appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@",labelText] attributes:nil]];
  287. text.yy_font = self.textFont;
  288. text.yy_color = self.textColor;
  289. return text;
  290. }
  291. @end