UUMarqueeView.m 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. //
  2. // UUMarqueeView.m
  3. // UUMarqueeView
  4. //
  5. // Created by youyou on 16/12/5.
  6. // Copyright © 2016年 iceyouyou. All rights reserved.
  7. //
  8. #import "UUMarqueeView.h"
  9. @interface UUMarqueeView () <UUMarqueeViewTouchResponder>
  10. @property (nonatomic, strong) UIView *contentView;
  11. @property (nonatomic, assign) NSInteger visibleItemCount;
  12. @property (nonatomic, strong) NSMutableArray<UUMarqueeItemView*> *items;
  13. @property (nonatomic, assign) int firstItemIndex;
  14. @property (nonatomic, assign) int dataIndex;
  15. @property (nonatomic, strong) NSTimer *scrollTimer;
  16. @property (nonatomic, strong) UUMarqueeViewTouchReceiver *touchReceiver;
  17. @property (nonatomic, assign) BOOL isWaiting;
  18. @property (nonatomic, assign) BOOL isScrolling;
  19. @property (nonatomic, assign) BOOL isScrollNeedsToStop;
  20. @property (nonatomic, assign) BOOL isPausingBeforeTouchesBegan;
  21. @property (nonatomic, assign) BOOL isPausingBeforeResignActive;
  22. @end
  23. @implementation UUMarqueeView
  24. static NSInteger const DEFAULT_VISIBLE_ITEM_COUNT = 2;
  25. static NSTimeInterval const DEFAULT_TIME_INTERVAL = 4.0;
  26. static NSTimeInterval const DEFAULT_TIME_DURATION = 1.0;
  27. static float const DEFAULT_SCROLL_SPEED = 40.0f;
  28. static float const DEFAULT_ITEM_SPACING = 20.0f;
  29. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  30. if (self = [super initWithCoder:aDecoder]) {
  31. _timeIntervalPerScroll = DEFAULT_TIME_INTERVAL;
  32. _timeDurationPerScroll = DEFAULT_TIME_DURATION;
  33. _scrollSpeed = DEFAULT_SCROLL_SPEED;
  34. _itemSpacing = DEFAULT_ITEM_SPACING;
  35. _touchEnabled = NO;
  36. _stopWhenLessData = NO;
  37. _contentView = [[UIView alloc] initWithFrame:self.bounds];
  38. _contentView.clipsToBounds = YES;
  39. [self addSubview:_contentView];
  40. [[NSNotificationCenter defaultCenter] addObserver:self
  41. selector:@selector(handleResignActiveNotification:)
  42. name:UIApplicationWillResignActiveNotification
  43. object:nil];
  44. [[NSNotificationCenter defaultCenter] addObserver:self
  45. selector:@selector(handleBecomeActiveNotification:)
  46. name:UIApplicationDidBecomeActiveNotification
  47. object:nil];
  48. }
  49. return self;
  50. }
  51. - (instancetype)initWithDirection:(UUMarqueeViewDirection)direction {
  52. if (self = [super init]) {
  53. _direction = direction;
  54. }
  55. return self;
  56. }
  57. - (instancetype)initWithFrame:(CGRect)frame direction:(UUMarqueeViewDirection)direction {
  58. if (self = [super initWithFrame:frame]) {
  59. _direction = direction;
  60. _timeIntervalPerScroll = DEFAULT_TIME_INTERVAL;
  61. _timeDurationPerScroll = DEFAULT_TIME_DURATION;
  62. _scrollSpeed = DEFAULT_SCROLL_SPEED;
  63. _itemSpacing = DEFAULT_ITEM_SPACING;
  64. _touchEnabled = NO;
  65. _stopWhenLessData = NO;
  66. _contentView = [[UIView alloc] initWithFrame:self.bounds];
  67. _contentView.clipsToBounds = YES;
  68. [self addSubview:_contentView];
  69. [[NSNotificationCenter defaultCenter] addObserver:self
  70. selector:@selector(handleResignActiveNotification:)
  71. name:UIApplicationWillResignActiveNotification
  72. object:nil];
  73. [[NSNotificationCenter defaultCenter] addObserver:self
  74. selector:@selector(handleBecomeActiveNotification:)
  75. name:UIApplicationDidBecomeActiveNotification
  76. object:nil];
  77. }
  78. return self;
  79. }
  80. - (instancetype)initWithFrame:(CGRect)frame {
  81. if (self = [super initWithFrame:frame]) {
  82. _timeIntervalPerScroll = DEFAULT_TIME_INTERVAL;
  83. _timeDurationPerScroll = DEFAULT_TIME_DURATION;
  84. _scrollSpeed = DEFAULT_SCROLL_SPEED;
  85. _itemSpacing = DEFAULT_ITEM_SPACING;
  86. _touchEnabled = NO;
  87. _stopWhenLessData = NO;
  88. _contentView = [[UIView alloc] initWithFrame:self.bounds];
  89. _contentView.clipsToBounds = YES;
  90. [self addSubview:_contentView];
  91. [[NSNotificationCenter defaultCenter] addObserver:self
  92. selector:@selector(handleResignActiveNotification:)
  93. name:UIApplicationWillResignActiveNotification
  94. object:nil];
  95. [[NSNotificationCenter defaultCenter] addObserver:self
  96. selector:@selector(handleBecomeActiveNotification:)
  97. name:UIApplicationDidBecomeActiveNotification
  98. object:nil];
  99. }
  100. return self;
  101. }
  102. - (void)setClipsToBounds:(BOOL)clipsToBounds {
  103. _contentView.clipsToBounds = clipsToBounds;
  104. }
  105. - (void)setTouchEnabled:(BOOL)touchEnabled {
  106. _touchEnabled = touchEnabled;
  107. [self resetTouchReceiver];
  108. }
  109. - (void)reloadData {
  110. if (_isWaiting) {
  111. if (_scrollTimer) {
  112. [_scrollTimer invalidate];
  113. self.scrollTimer = nil;
  114. }
  115. [self resetAll];
  116. [self startAfterTimeInterval:YES];
  117. } else if (_isScrolling) {
  118. [self resetAll];
  119. } else {
  120. // stopped
  121. [self resetAll];
  122. [self startAfterTimeInterval:YES];
  123. }
  124. }
  125. - (void)start {
  126. self.isScrollNeedsToStop = NO;
  127. if (!_isScrolling && !_isWaiting) {
  128. [self startAfterTimeInterval:NO];
  129. }
  130. }
  131. - (void)pause {
  132. self.isScrollNeedsToStop = YES;
  133. }
  134. - (void)repeat {
  135. if (!_isScrollNeedsToStop) {
  136. [self startAfterTimeInterval:YES];
  137. }
  138. }
  139. - (void)repeatWithAnimationFinished:(BOOL)finished {
  140. if (!_isScrollNeedsToStop) {
  141. [self startAfterTimeInterval:YES animationFinished:finished];
  142. }
  143. }
  144. - (void)startAfterTimeInterval:(BOOL)afterTimeInterval {
  145. [self startAfterTimeInterval:afterTimeInterval animationFinished:YES];
  146. }
  147. - (void)startAfterTimeInterval:(BOOL)afterTimeInterval animationFinished:(BOOL)finished {
  148. if (_isScrolling || _items.count <= 0) {
  149. return;
  150. }
  151. self.isWaiting = YES;
  152. NSTimeInterval timeInterval = 1.0;
  153. if (finished) {
  154. timeInterval = afterTimeInterval ? _timeIntervalPerScroll : 0.0;
  155. }
  156. self.scrollTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval
  157. target:self
  158. selector:@selector(scrollTimerDidFire:)
  159. userInfo:nil
  160. repeats:NO];
  161. }
  162. #pragma mark - Override(private)
  163. - (void)layoutSubviews {
  164. [super layoutSubviews];
  165. [_contentView setFrame:self.bounds];
  166. [self repositionItemViews];
  167. if (_touchEnabled && _touchReceiver) {
  168. [_touchReceiver setFrame:self.bounds];
  169. }
  170. }
  171. - (void)dealloc {
  172. if (_scrollTimer) {
  173. [_scrollTimer invalidate];
  174. self.scrollTimer = nil;
  175. }
  176. [[NSNotificationCenter defaultCenter] removeObserver:self];
  177. }
  178. #pragma mark - Notification
  179. - (void)handleResignActiveNotification:(NSNotification*)notification {
  180. self.isPausingBeforeResignActive = _isScrollNeedsToStop;
  181. [self pause];
  182. }
  183. - (void)handleBecomeActiveNotification:(NSNotification*)notification {
  184. if (!_isPausingBeforeResignActive) {
  185. [self start];
  186. }
  187. }
  188. #pragma mark - ItemView(private)
  189. - (void)resetAll {
  190. self.dataIndex = -1;
  191. self.firstItemIndex = 0;
  192. if (_items) {
  193. [_items makeObjectsPerformSelector:@selector(removeFromSuperview)];
  194. [_items removeAllObjects];
  195. } else {
  196. self.items = [NSMutableArray array];
  197. }
  198. if (_direction == UUMarqueeViewDirectionLeftward) {
  199. self.visibleItemCount = 1;
  200. } else {
  201. if ([_delegate respondsToSelector:@selector(numberOfVisibleItemsForMarqueeView:)]) {
  202. self.visibleItemCount = [_delegate numberOfVisibleItemsForMarqueeView:self];
  203. if (_visibleItemCount <= 0) {
  204. return;
  205. }
  206. } else {
  207. self.visibleItemCount = DEFAULT_VISIBLE_ITEM_COUNT;
  208. }
  209. }
  210. for (int i = 0; i < _visibleItemCount + 2; i++) {
  211. UUMarqueeItemView *itemView = [[UUMarqueeItemView alloc] init];
  212. [_contentView addSubview:itemView];
  213. [_items addObject:itemView];
  214. }
  215. if (_direction == UUMarqueeViewDirectionLeftward) {
  216. CGFloat itemHeight = CGRectGetHeight(self.frame) / _visibleItemCount;
  217. CGFloat lastMaxX = 0.0f;
  218. for (int i = 0; i < _items.count; i++) {
  219. int index = (i + _firstItemIndex) % _items.count;
  220. CGFloat itemWidth = CGRectGetWidth(self.frame);
  221. if (i == 0) {
  222. [_items[index] setFrame:CGRectMake(-itemWidth, 0.0f, itemWidth, itemHeight)];
  223. lastMaxX = 0.0f;
  224. [self createItemView:_items[index]];
  225. } else {
  226. [self moveToNextDataIndex];
  227. _items[index].tag = _dataIndex;
  228. _items[index].width = [self itemWidthAtIndex:_items[index].tag];
  229. itemWidth = MAX(_items[index].width + _itemSpacing, itemWidth);
  230. [_items[index] setFrame:CGRectMake(lastMaxX, 0.0f, itemWidth, itemHeight)];
  231. lastMaxX = lastMaxX + itemWidth;
  232. [self updateItemView:_items[index] atIndex:_items[index].tag];
  233. }
  234. }
  235. } else {
  236. if (_useDynamicHeight) {
  237. CGFloat itemWidth = CGRectGetWidth(self.frame);
  238. for (int i = 0; i < _items.count; i++) {
  239. int index = (i + _firstItemIndex) % _items.count;
  240. if (i == _items.count - 1) {
  241. [self moveToNextDataIndex];
  242. _items[index].tag = _dataIndex;
  243. _items[index].height = [self itemHeightAtIndex:_items[index].tag];
  244. _items[index].alpha = 0.0f;
  245. [_items[index] setFrame:CGRectMake(0.0f, CGRectGetMaxY(self.bounds), itemWidth, _items[index].height)];
  246. [self updateItemView:_items[index] atIndex:_items[index].tag];
  247. } else {
  248. _items[index].tag = _dataIndex;
  249. _items[index].alpha = 0.0f;
  250. [_items[index] setFrame:CGRectMake(0.0f, 0.0f, itemWidth, 0.0f)];
  251. }
  252. }
  253. } else {
  254. NSUInteger dataCount = 0;
  255. if ([_delegate respondsToSelector:@selector(numberOfDataForMarqueeView:)]) {
  256. dataCount = [_delegate numberOfDataForMarqueeView:self];
  257. }
  258. CGFloat itemWidth = CGRectGetWidth(self.frame);
  259. CGFloat itemHeight = CGRectGetHeight(self.frame) / _visibleItemCount;
  260. for (int i = 0; i < _items.count; i++) {
  261. int index = (i + _firstItemIndex) % _items.count;
  262. if (i == 0) {
  263. _items[index].tag = _dataIndex;
  264. [_items[index] setFrame:CGRectMake(0.0f, -itemHeight, itemWidth, itemHeight)];
  265. [self createItemView:_items[index]];
  266. } else {
  267. [self moveToNextDataIndex];
  268. _items[index].tag = _dataIndex;
  269. [_items[index] setFrame:CGRectMake(0.0f, itemHeight * (i - 1), itemWidth, itemHeight)];
  270. if (_stopWhenLessData) {
  271. if (i <= dataCount) {
  272. [self updateItemView:_items[index] atIndex:_items[index].tag];
  273. } else {
  274. [self createItemView:_items[index]];
  275. }
  276. } else {
  277. [self updateItemView:_items[index] atIndex:_items[index].tag];
  278. }
  279. }
  280. }
  281. }
  282. }
  283. [self resetTouchReceiver];
  284. }
  285. - (void)repositionItemViews {
  286. if (_direction == UUMarqueeViewDirectionLeftward) {
  287. CGFloat itemHeight = CGRectGetHeight(self.frame) / _visibleItemCount;
  288. CGFloat lastMaxX = 0.0f;
  289. for (int i = 0; i < _items.count; i++) {
  290. int index = (i + _firstItemIndex) % _items.count;
  291. CGFloat itemWidth = MAX(_items[index].width + _itemSpacing, CGRectGetWidth(self.frame));
  292. if (i == 0) {
  293. [_items[index] setFrame:CGRectMake(-itemWidth, 0.0f, itemWidth, itemHeight)];
  294. lastMaxX = 0.0f;
  295. } else {
  296. [_items[index] setFrame:CGRectMake(lastMaxX, 0.0f, itemWidth, itemHeight)];
  297. lastMaxX = lastMaxX + itemWidth;
  298. }
  299. }
  300. } else {
  301. if (_useDynamicHeight) {
  302. CGFloat itemWidth = CGRectGetWidth(self.frame);
  303. CGFloat lastMaxY = 0.0f;
  304. for (int i = 0; i < _items.count; i++) {
  305. int index = (i + _firstItemIndex) % _items.count;
  306. if (i == 0) {
  307. [_items[index] setFrame:CGRectMake(0.0f, 0.0f, itemWidth, 0.0f)];
  308. lastMaxY = 0.0f;
  309. } else if (i == _items.count - 1) {
  310. [_items[index] setFrame:CGRectMake(0.0f, CGRectGetMaxY(self.bounds), itemWidth, _items[index].height)];
  311. } else {
  312. [_items[index] setFrame:CGRectMake(0.0f, lastMaxY, itemWidth, _items[index].height)];
  313. lastMaxY = lastMaxY + _items[index].height;
  314. }
  315. }
  316. CGFloat offsetY = CGRectGetHeight(self.frame) - lastMaxY;
  317. for (int i = 0; i < _items.count; i++) {
  318. int index = (i + _firstItemIndex) % _items.count;
  319. if (i > 0 && i < _items.count - 1) {
  320. [_items[index] setFrame:CGRectMake(CGRectGetMinX(_items[index].frame),
  321. CGRectGetMinY(_items[index].frame) + offsetY,
  322. itemWidth,
  323. _items[index].height)];
  324. }
  325. }
  326. } else {
  327. CGFloat itemWidth = CGRectGetWidth(self.frame);
  328. CGFloat itemHeight = CGRectGetHeight(self.frame) / _visibleItemCount;
  329. for (int i = 0; i < _items.count; i++) {
  330. int index = (i + _firstItemIndex) % _items.count;
  331. if (i == 0) {
  332. [_items[index] setFrame:CGRectMake(0.0f, -itemHeight, itemWidth, itemHeight)];
  333. } else {
  334. [_items[index] setFrame:CGRectMake(0.0f, itemHeight * (i - 1), itemWidth, itemHeight)];
  335. }
  336. }
  337. }
  338. }
  339. }
  340. - (int)itemIndexWithOffsetFromTop:(int)offsetFromTop {
  341. return (_firstItemIndex + offsetFromTop) % (_visibleItemCount + 2);
  342. }
  343. - (void)moveToNextItemIndex {
  344. if (_firstItemIndex >= _items.count - 1) {
  345. self.firstItemIndex = 0;
  346. } else {
  347. self.firstItemIndex++;
  348. }
  349. }
  350. - (CGFloat)itemWidthAtIndex:(NSInteger)index {
  351. CGFloat itemWidth = 0.0f;
  352. if (index >= 0) {
  353. if ([_delegate respondsToSelector:@selector(itemViewWidthAtIndex:forMarqueeView:)]) {
  354. itemWidth = [_delegate itemViewWidthAtIndex:index forMarqueeView:self];
  355. }
  356. }
  357. return itemWidth;
  358. }
  359. - (CGFloat)itemHeightAtIndex:(NSInteger)index {
  360. CGFloat itemHeight = 0.0f;
  361. if (index >= 0) {
  362. if ([_delegate respondsToSelector:@selector(itemViewHeightAtIndex:forMarqueeView:)]) {
  363. itemHeight = [_delegate itemViewHeightAtIndex:index forMarqueeView:self];
  364. }
  365. }
  366. return itemHeight;
  367. }
  368. - (void)createItemView:(UUMarqueeItemView*)itemView {
  369. if (!itemView.didFinishCreate) {
  370. if ([_delegate respondsToSelector:@selector(createItemView:forMarqueeView:)]) {
  371. [_delegate createItemView:itemView forMarqueeView:self];
  372. itemView.didFinishCreate = YES;
  373. }
  374. }
  375. }
  376. - (void)updateItemView:(UUMarqueeItemView*)itemView atIndex:(NSInteger)index {
  377. if (index < 0) {
  378. [itemView clear];
  379. }
  380. if (!itemView.didFinishCreate) {
  381. if ([_delegate respondsToSelector:@selector(createItemView:forMarqueeView:)]) {
  382. [_delegate createItemView:itemView forMarqueeView:self];
  383. itemView.didFinishCreate = YES;
  384. }
  385. }
  386. if (index >= 0) {
  387. if ([_delegate respondsToSelector:@selector(updateItemView:atIndex:forMarqueeView:)]) {
  388. [_delegate updateItemView:itemView atIndex:index forMarqueeView:self];
  389. }
  390. }
  391. }
  392. #pragma mark - Timer & Animation(private)
  393. - (void)scrollTimerDidFire:(NSTimer *)timer {
  394. self.isWaiting = NO;
  395. if (_isScrollNeedsToStop) {
  396. return;
  397. }
  398. self.isScrolling = YES;
  399. if (_stopWhenLessData) {
  400. NSUInteger dataCount = 0;
  401. if ([_delegate respondsToSelector:@selector(numberOfDataForMarqueeView:)]) {
  402. dataCount = [_delegate numberOfDataForMarqueeView:self];
  403. }
  404. if (_direction == UUMarqueeViewDirectionLeftward) {
  405. if (dataCount <= 1) {
  406. CGFloat itemWidth = MAX(_items[1].width + _itemSpacing, CGRectGetWidth(self.frame));
  407. if (itemWidth <= CGRectGetWidth(self.frame)) {
  408. __weak __typeof(self) weakSelf = self;
  409. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_timeDurationPerScroll * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  410. __strong __typeof(self) self = weakSelf;
  411. if (self) {
  412. self.isScrolling = NO;
  413. [self repeat];
  414. }
  415. });
  416. return;
  417. }
  418. }
  419. } else {
  420. if (dataCount <= _visibleItemCount) {
  421. __weak __typeof(self) weakSelf = self;
  422. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_timeDurationPerScroll * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  423. __strong __typeof(self) self = weakSelf;
  424. if (self) {
  425. self.isScrolling = NO;
  426. [self repeat];
  427. }
  428. });
  429. return;
  430. }
  431. }
  432. }
  433. dispatch_async(dispatch_get_main_queue(), ^() {
  434. if (_direction == UUMarqueeViewDirectionLeftward) {
  435. [self moveToNextDataIndex];
  436. CGFloat itemHeight = CGRectGetHeight(self.frame);
  437. CGFloat firstItemWidth = CGRectGetWidth(self.frame);
  438. CGFloat currentItemWidth = CGRectGetWidth(self.frame);
  439. CGFloat lastItemWidth = CGRectGetWidth(self.frame);
  440. for (int i = 0; i < _items.count; i++) {
  441. int index = (i + _firstItemIndex) % _items.count;
  442. CGFloat itemWidth = MAX(_items[index].width + _itemSpacing, CGRectGetWidth(self.frame));
  443. if (i == 0) {
  444. firstItemWidth = itemWidth;
  445. } else if (i == 1) {
  446. currentItemWidth = itemWidth;
  447. } else {
  448. lastItemWidth = itemWidth;
  449. }
  450. }
  451. // move the left item to right without animation
  452. _items[_firstItemIndex].tag = _dataIndex;
  453. _items[_firstItemIndex].width = [self itemWidthAtIndex:_items[_firstItemIndex].tag];
  454. CGFloat nextItemWidth = MAX(_items[_firstItemIndex].width + _itemSpacing, CGRectGetWidth(self.frame));
  455. [_items[_firstItemIndex] setFrame:CGRectMake(lastItemWidth, 0.0f, nextItemWidth, itemHeight)];
  456. if (firstItemWidth != nextItemWidth) {
  457. // if the width of next item view changes, then recreate it by delegate
  458. [_items[_firstItemIndex] clear];
  459. }
  460. [self updateItemView:_items[_firstItemIndex] atIndex:_items[_firstItemIndex].tag];
  461. __weak __typeof(self) weakSelf = self;
  462. [UIView animateWithDuration:(currentItemWidth / _scrollSpeed) delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
  463. CGFloat lastMaxX = 0.0f;
  464. for (int i = 0; i < _items.count; i++) {
  465. int index = (i + _firstItemIndex) % _items.count;
  466. CGFloat itemWidth = MAX(_items[index].width + _itemSpacing, CGRectGetWidth(self.frame));
  467. if (i == 0) {
  468. continue;
  469. } else if (i == 1) {
  470. [_items[index] setFrame:CGRectMake(-itemWidth, 0.0f, itemWidth, itemHeight)];
  471. lastMaxX = 0.0f;
  472. } else {
  473. [_items[index] setFrame:CGRectMake(lastMaxX, 0.0f, itemWidth, itemHeight)];
  474. lastMaxX = lastMaxX + itemWidth;
  475. }
  476. }
  477. } completion:^(BOOL finished) {
  478. __strong __typeof(self) self = weakSelf;
  479. if (self) {
  480. self.isScrolling = NO;
  481. [self repeatWithAnimationFinished:finished];
  482. }
  483. }];
  484. [self moveToNextItemIndex];
  485. } else {
  486. [self moveToNextDataIndex];
  487. CGFloat itemWidth = CGRectGetWidth(self.frame);
  488. CGFloat itemHeight = CGRectGetHeight(self.frame) / _visibleItemCount;
  489. // move the top item to bottom without animation
  490. _items[_firstItemIndex].tag = _dataIndex;
  491. if (_useDynamicHeight) {
  492. CGFloat firstItemWidth = _items[_firstItemIndex].height;
  493. _items[_firstItemIndex].height = [self itemHeightAtIndex:_items[_firstItemIndex].tag];
  494. [_items[_firstItemIndex] setFrame:CGRectMake(0.0f, CGRectGetMaxY(self.bounds), itemWidth, _items[_firstItemIndex].height)];
  495. if (firstItemWidth != _items[_firstItemIndex].height) {
  496. // if the height of next item view changes, then recreate it by delegate
  497. [_items[_firstItemIndex] clear];
  498. }
  499. } else {
  500. [_items[_firstItemIndex] setFrame:CGRectMake(0.0f, CGRectGetMaxY(self.bounds), itemWidth, itemHeight)];
  501. }
  502. [self updateItemView:_items[_firstItemIndex] atIndex:_items[_firstItemIndex].tag];
  503. if (_useDynamicHeight) {
  504. int lastItemIndex = (int)(_items.count - 1 + _firstItemIndex) % _items.count;
  505. CGFloat lastItemHeight = _items[lastItemIndex].height;
  506. __weak __typeof(self) weakSelf = self;
  507. [UIView animateWithDuration:(lastItemHeight / _scrollSpeed) delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
  508. for (int i = 0; i < _items.count; i++) {
  509. int index = (i + _firstItemIndex) % _items.count;
  510. if (i == 0) {
  511. continue;
  512. } else if (i == 1) {
  513. [_items[index] setFrame:CGRectMake(CGRectGetMinX(_items[index].frame),
  514. CGRectGetMinY(_items[index].frame) - lastItemHeight,
  515. itemWidth,
  516. _items[index].height)];
  517. _items[index].alpha = 0.0f;
  518. } else {
  519. [_items[index] setFrame:CGRectMake(CGRectGetMinX(_items[index].frame),
  520. CGRectGetMinY(_items[index].frame) - lastItemHeight,
  521. itemWidth,
  522. _items[index].height)];
  523. _items[index].alpha = 1.0f;
  524. }
  525. }
  526. } completion:^(BOOL finished) {
  527. __strong __typeof(self) self = weakSelf;
  528. if (self) {
  529. self.isScrolling = NO;
  530. [self repeatWithAnimationFinished:finished];
  531. }
  532. }];
  533. } else {
  534. UIViewAnimationOptions animationOptions = UIViewAnimationOptionCurveEaseInOut;
  535. if (_timeIntervalPerScroll <= 0.0) {
  536. // smooth animation
  537. animationOptions = UIViewAnimationOptionCurveLinear;
  538. }
  539. __weak __typeof(self) weakSelf = self;
  540. [UIView animateWithDuration:_timeDurationPerScroll delay:0.0 options:animationOptions animations:^{
  541. for (int i = 0; i < _items.count; i++) {
  542. int index = (i + _firstItemIndex) % _items.count;
  543. if (i == 0) {
  544. continue;
  545. } else if (i == 1) {
  546. [_items[index] setFrame:CGRectMake(0.0f, -itemHeight, itemWidth, itemHeight)];
  547. } else {
  548. [_items[index] setFrame:CGRectMake(0.0f, itemHeight * (i - 2), itemWidth, itemHeight)];
  549. }
  550. }
  551. } completion:^(BOOL finished) {
  552. __strong __typeof(self) self = weakSelf;
  553. if (self) {
  554. self.isScrolling = NO;
  555. [self repeatWithAnimationFinished:finished];
  556. }
  557. }];
  558. }
  559. [self moveToNextItemIndex];
  560. }
  561. });
  562. }
  563. #pragma mark - Data source(private)
  564. - (void)moveToNextDataIndex {
  565. NSUInteger dataCount = 0;
  566. if ([_delegate respondsToSelector:@selector(numberOfDataForMarqueeView:)]) {
  567. dataCount = [_delegate numberOfDataForMarqueeView:self];
  568. }
  569. if (dataCount <= 0) {
  570. self.dataIndex = -1;
  571. } else {
  572. self.dataIndex = _dataIndex + 1;
  573. if (_dataIndex < 0 || _dataIndex > dataCount - 1) {
  574. self.dataIndex = 0;
  575. }
  576. }
  577. }
  578. #pragma mark - Touch handler(private)
  579. - (void)resetTouchReceiver {
  580. if (_touchEnabled) {
  581. if (!_touchReceiver) {
  582. self.touchReceiver = [[UUMarqueeViewTouchReceiver alloc] init];
  583. _touchReceiver.touchDelegate = self;
  584. [self addSubview:_touchReceiver];
  585. } else {
  586. [self bringSubviewToFront:_touchReceiver];
  587. }
  588. } else {
  589. if (_touchReceiver) {
  590. [_touchReceiver removeFromSuperview];
  591. self.touchReceiver = nil;
  592. }
  593. }
  594. }
  595. #pragma mark - UUMarqueeViewTouchResponder(private)
  596. - (void)touchesBegan {
  597. self.isPausingBeforeTouchesBegan = _isScrollNeedsToStop;
  598. [self pause];
  599. }
  600. - (void)touchesEndedAtPoint:(CGPoint)point {
  601. for (UUMarqueeItemView *itemView in _items) {
  602. if ([itemView.layer.presentationLayer hitTest:point]) {
  603. NSUInteger dataCount = 0;
  604. if ([_delegate respondsToSelector:@selector(numberOfDataForMarqueeView:)]) {
  605. dataCount = [_delegate numberOfDataForMarqueeView:self];
  606. }
  607. if (dataCount > 0 && itemView.tag >= 0 && itemView.tag < dataCount) {
  608. if ([self.delegate respondsToSelector:@selector(didTouchItemViewAtIndex:forMarqueeView:)]) {
  609. [self.delegate didTouchItemViewAtIndex:itemView.tag forMarqueeView:self];
  610. }
  611. }
  612. break;
  613. }
  614. }
  615. if (!_isPausingBeforeTouchesBegan) {
  616. [self start];
  617. }
  618. }
  619. - (void)touchesCancelled {
  620. if (!_isPausingBeforeTouchesBegan) {
  621. [self start];
  622. }
  623. }
  624. @end
  625. #pragma mark - UUMarqueeViewTouchReceiver(private)
  626. @implementation UUMarqueeViewTouchReceiver
  627. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  628. if (_touchDelegate) {
  629. [_touchDelegate touchesBegan];
  630. }
  631. }
  632. - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  633. UITouch *touch = [touches anyObject];
  634. CGPoint touchLocation = [touch locationInView:self];
  635. if (_touchDelegate) {
  636. [_touchDelegate touchesEndedAtPoint:touchLocation];
  637. }
  638. }
  639. - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  640. if (_touchDelegate) {
  641. [_touchDelegate touchesCancelled];
  642. }
  643. }
  644. @end
  645. #pragma mark - UUMarqueeItemView(Private)
  646. @implementation UUMarqueeItemView
  647. - (void)clear {
  648. [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  649. _didFinishCreate = NO;
  650. }
  651. @end