UIScrollView+ZFPlayer.m 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. //
  2. // UIScrollView+ZFPlayer.m
  3. // ZFPlayer
  4. //
  5. // Copyright (c) 2016年 任子丰 ( http://github.com/renzifeng )
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #import "UIScrollView+ZFPlayer.h"
  25. #import <objc/runtime.h>
  26. #import "ZFReachabilityManager.h"
  27. #import "ZFPlayer.h"
  28. #pragma clang diagnostic push
  29. #pragma clang diagnostic ignored"-Wdeprecated-declarations"
  30. @interface UIScrollView ()
  31. @property (nonatomic) CGFloat zf_lastOffsetY;
  32. @property (nonatomic) CGFloat zf_lastOffsetX;
  33. @property (nonatomic) ZFPlayerScrollDirection zf_scrollDirection;
  34. @end
  35. @implementation UIScrollView (ZFPlayer)
  36. + (void)load {
  37. static dispatch_once_t onceToken;
  38. dispatch_once(&onceToken, ^{
  39. SEL selectors[] = {
  40. @selector(setContentOffset:)
  41. };
  42. for (NSInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
  43. SEL originalSelector = selectors[index];
  44. SEL swizzledSelector = NSSelectorFromString([@"zf_" stringByAppendingString:NSStringFromSelector(originalSelector)]);
  45. Method originalMethod = class_getInstanceMethod(self, originalSelector);
  46. Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector);
  47. if (class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))) {
  48. class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
  49. } else {
  50. method_exchangeImplementations(originalMethod, swizzledMethod);
  51. }
  52. }
  53. });
  54. }
  55. - (void)zf_setContentOffset:(CGPoint)contentOffset {
  56. if (self.zf_scrollViewDirection == ZFPlayerScrollViewDirectionVertical) {
  57. [self _findCorrectCellWhenScrollViewDirectionVertical:nil];
  58. } else {
  59. [self _findCorrectCellWhenScrollViewDirectionHorizontal:nil];
  60. }
  61. [self zf_setContentOffset:contentOffset];
  62. }
  63. #pragma mark - private method
  64. - (void)_scrollViewDidStopScroll {
  65. @weakify(self)
  66. [self zf_filterShouldPlayCellWhileScrolled:^(NSIndexPath * _Nonnull indexPath) {
  67. @strongify(self)
  68. if (self.zf_scrollViewDidStopScrollCallback) self.zf_scrollViewDidStopScrollCallback(indexPath);
  69. if (self.zf_scrollViewDidEndScrollingCallback) self.zf_scrollViewDidEndScrollingCallback(indexPath);
  70. }];
  71. }
  72. - (void)_scrollViewBeginDragging {
  73. if (self.zf_scrollViewDirection == ZFPlayerScrollViewDirectionVertical) {
  74. self.zf_lastOffsetY = self.contentOffset.y;
  75. } else {
  76. self.zf_lastOffsetX = self.contentOffset.x;
  77. }
  78. }
  79. /**
  80. The percentage of scrolling processed in vertical scrolling.
  81. */
  82. - (void)_scrollViewScrollingDirectionVertical {
  83. CGFloat offsetY = self.contentOffset.y;
  84. self.zf_scrollDirection = (offsetY - self.zf_lastOffsetY > 0) ? ZFPlayerScrollDirectionUp : ZFPlayerScrollDirectionDown;
  85. self.zf_lastOffsetY = offsetY;
  86. if (self.zf_stopPlay) return;
  87. UIView *playerView;
  88. if (self.zf_containerType == ZFPlayerContainerTypeCell) {
  89. // Avoid being paused the first time you play it.
  90. if (self.contentOffset.y < 0) return;
  91. if (!self.zf_playingIndexPath) return;
  92. UIView *cell = [self zf_getCellForIndexPath:self.zf_playingIndexPath];
  93. if (!cell) {
  94. if (self.zf_playerDidDisappearInScrollView) self.zf_playerDidDisappearInScrollView(self.zf_playingIndexPath);
  95. return;
  96. }
  97. playerView = [cell viewWithTag:self.zf_containerViewTag];
  98. } else if (self.zf_containerType == ZFPlayerContainerTypeView) {
  99. if (!self.zf_containerView) return;
  100. playerView = self.zf_containerView;
  101. }
  102. CGRect rect1 = [playerView convertRect:playerView.frame toView:self];
  103. CGRect rect = [self convertRect:rect1 toView:self.superview];
  104. /// playerView top to scrollView top space.
  105. CGFloat topSpacing = CGRectGetMinY(rect) - CGRectGetMinY(self.frame) - CGRectGetMinY(playerView.frame);
  106. /// playerView bottom to scrollView bottom space.
  107. CGFloat bottomSpacing = CGRectGetMaxY(self.frame) - CGRectGetMaxY(rect) + CGRectGetMinY(playerView.frame);
  108. /// The height of the content area.
  109. CGFloat contentInsetHeight = CGRectGetMaxY(self.frame) - CGRectGetMinY(self.frame);
  110. CGFloat playerDisapperaPercent = 0;
  111. CGFloat playerApperaPercent = 0;
  112. if (self.zf_scrollDirection == ZFPlayerScrollDirectionUp) { /// Scroll up
  113. /// Player is disappearing.
  114. if (topSpacing <= 0 && CGRectGetHeight(rect) != 0) {
  115. playerDisapperaPercent = -topSpacing/CGRectGetHeight(rect);
  116. if (playerDisapperaPercent > 1.0) playerDisapperaPercent = 1.0;
  117. if (self.zf_playerDisappearingInScrollView) self.zf_playerDisappearingInScrollView(self.zf_playingIndexPath, playerDisapperaPercent);
  118. }
  119. /// Top area
  120. if (topSpacing <= 0 && topSpacing > -CGRectGetHeight(rect)/2) {
  121. /// When the player will disappear.
  122. if (self.zf_playerWillDisappearInScrollView) self.zf_playerWillDisappearInScrollView(self.zf_playingIndexPath);
  123. } else if (topSpacing <= -CGRectGetHeight(rect)) {
  124. /// When the player did disappeared.
  125. if (self.zf_playerDidDisappearInScrollView) self.zf_playerDidDisappearInScrollView(self.zf_playingIndexPath);
  126. } else if (topSpacing > 0 && topSpacing <= contentInsetHeight) {
  127. /// Player is appearing.
  128. if (CGRectGetHeight(rect) != 0) {
  129. playerApperaPercent = -(topSpacing-contentInsetHeight)/CGRectGetHeight(rect);
  130. if (playerApperaPercent > 1.0) playerApperaPercent = 1.0;
  131. if (self.zf_playerAppearingInScrollView) self.zf_playerAppearingInScrollView(self.zf_playingIndexPath, playerApperaPercent);
  132. }
  133. /// In visable area
  134. if (topSpacing <= contentInsetHeight && topSpacing > contentInsetHeight-CGRectGetHeight(rect)/2) {
  135. /// When the player will appear.
  136. if (self.zf_playerWillAppearInScrollView) self.zf_playerWillAppearInScrollView(self.zf_playingIndexPath);
  137. } else {
  138. /// When the player did appeared.
  139. if (self.zf_playerDidAppearInScrollView) self.zf_playerDidAppearInScrollView(self.zf_playingIndexPath);
  140. }
  141. }
  142. } else if (self.zf_scrollDirection == ZFPlayerScrollDirectionDown) { /// Scroll Down
  143. /// Player is disappearing.
  144. if (bottomSpacing <= 0 && CGRectGetHeight(rect) != 0) {
  145. playerDisapperaPercent = -bottomSpacing/CGRectGetHeight(rect);
  146. if (playerDisapperaPercent > 1.0) playerDisapperaPercent = 1.0;
  147. if (self.zf_playerDisappearingInScrollView) self.zf_playerDisappearingInScrollView(self.zf_playingIndexPath, playerDisapperaPercent);
  148. }
  149. /// Bottom area
  150. if (bottomSpacing <= 0 && bottomSpacing > -CGRectGetHeight(rect)/2) {
  151. /// When the player will disappear.
  152. if (self.zf_playerWillDisappearInScrollView) self.zf_playerWillDisappearInScrollView(self.zf_playingIndexPath);
  153. } else if (bottomSpacing <= -CGRectGetHeight(rect)) {
  154. /// When the player did disappeared.
  155. if (self.zf_playerDidDisappearInScrollView) self.zf_playerDidDisappearInScrollView(self.zf_playingIndexPath);
  156. } else if (bottomSpacing > 0 && bottomSpacing <= contentInsetHeight) {
  157. /// Player is appearing.
  158. if (CGRectGetHeight(rect) != 0) {
  159. playerApperaPercent = -(bottomSpacing-contentInsetHeight)/CGRectGetHeight(rect);
  160. if (playerApperaPercent > 1.0) playerApperaPercent = 1.0;
  161. if (self.zf_playerAppearingInScrollView) self.zf_playerAppearingInScrollView(self.zf_playingIndexPath, playerApperaPercent);
  162. }
  163. /// In visable area
  164. if (bottomSpacing <= contentInsetHeight && bottomSpacing > contentInsetHeight-CGRectGetHeight(rect)/2) {
  165. /// When the player will appear.
  166. if (self.zf_playerWillAppearInScrollView) self.zf_playerWillAppearInScrollView(self.zf_playingIndexPath);
  167. } else {
  168. /// When the player did appeared.
  169. if (self.zf_playerDidAppearInScrollView) self.zf_playerDidAppearInScrollView(self.zf_playingIndexPath);
  170. }
  171. }
  172. }
  173. }
  174. /**
  175. The percentage of scrolling processed in horizontal scrolling.
  176. */
  177. - (void)_scrollViewScrollingDirectionHorizontal {
  178. CGFloat offsetX = self.contentOffset.x;
  179. self.zf_scrollDirection = (offsetX - self.zf_lastOffsetX > 0) ? ZFPlayerScrollDirectionLeft : ZFPlayerScrollDirectionRight;
  180. self.zf_lastOffsetX = offsetX;
  181. if (self.zf_stopPlay) return;
  182. UIView *playerView;
  183. if (self.zf_containerType == ZFPlayerContainerTypeCell) {
  184. // Avoid being paused the first time you play it.
  185. if (self.contentOffset.x < 0) return;
  186. if (!self.zf_playingIndexPath) return;
  187. UIView *cell = [self zf_getCellForIndexPath:self.zf_playingIndexPath];
  188. if (!cell) {
  189. if (self.zf_playerDidDisappearInScrollView) self.zf_playerDidDisappearInScrollView(self.zf_playingIndexPath);
  190. return;
  191. }
  192. playerView = [cell viewWithTag:self.zf_containerViewTag];
  193. } else if (self.zf_containerType == ZFPlayerContainerTypeView) {
  194. if (!self.zf_containerView) return;
  195. playerView = self.zf_containerView;
  196. }
  197. CGRect rect1 = [playerView convertRect:playerView.frame toView:self];
  198. CGRect rect = [self convertRect:rect1 toView:self.superview];
  199. /// playerView left to scrollView left space.
  200. CGFloat leftSpacing = CGRectGetMinX(rect) - CGRectGetMinX(self.frame) - CGRectGetMinX(playerView.frame);
  201. /// playerView bottom to scrollView right space.
  202. CGFloat rightSpacing = CGRectGetMaxX(self.frame) - CGRectGetMaxX(rect) + CGRectGetMinX(playerView.frame);
  203. /// The height of the content area.
  204. CGFloat contentInsetWidth = CGRectGetMaxX(self.frame) - CGRectGetMinX(self.frame);
  205. CGFloat playerDisapperaPercent = 0;
  206. CGFloat playerApperaPercent = 0;
  207. if (self.zf_scrollDirection == ZFPlayerScrollDirectionLeft) { /// Scroll left
  208. /// Player is disappearing.
  209. if (leftSpacing <= 0 && CGRectGetWidth(rect) != 0) {
  210. playerDisapperaPercent = -leftSpacing/CGRectGetWidth(rect);
  211. if (playerDisapperaPercent > 1.0) playerDisapperaPercent = 1.0;
  212. if (self.zf_playerDisappearingInScrollView) self.zf_playerDisappearingInScrollView(self.zf_playingIndexPath, playerDisapperaPercent);
  213. }
  214. /// Top area
  215. if (leftSpacing <= 0 && leftSpacing > -CGRectGetWidth(rect)/2) {
  216. /// When the player will disappear.
  217. if (self.zf_playerWillDisappearInScrollView) self.zf_playerWillDisappearInScrollView(self.zf_playingIndexPath);
  218. } else if (leftSpacing <= -CGRectGetWidth(rect)) {
  219. /// When the player did disappeared.
  220. if (self.zf_playerDidDisappearInScrollView) self.zf_playerDidDisappearInScrollView(self.zf_playingIndexPath);
  221. } else if (leftSpacing > 0 && leftSpacing <= contentInsetWidth) {
  222. /// Player is appearing.
  223. if (CGRectGetWidth(rect) != 0) {
  224. playerApperaPercent = -(leftSpacing-contentInsetWidth)/CGRectGetWidth(rect);
  225. if (playerApperaPercent > 1.0) playerApperaPercent = 1.0;
  226. if (self.zf_playerAppearingInScrollView) self.zf_playerAppearingInScrollView(self.zf_playingIndexPath, playerApperaPercent);
  227. }
  228. /// In visable area
  229. if (leftSpacing <= contentInsetWidth && leftSpacing > contentInsetWidth-CGRectGetWidth(rect)/2) {
  230. /// When the player will appear.
  231. if (self.zf_playerWillAppearInScrollView) self.zf_playerWillAppearInScrollView(self.zf_playingIndexPath);
  232. } else {
  233. /// When the player did appeared.
  234. if (self.zf_playerDidAppearInScrollView) self.zf_playerDidAppearInScrollView(self.zf_playingIndexPath);
  235. }
  236. }
  237. } else if (self.zf_scrollDirection == ZFPlayerScrollDirectionRight) { /// Scroll right
  238. /// Player is disappearing.
  239. if (rightSpacing <= 0 && CGRectGetWidth(rect) != 0) {
  240. playerDisapperaPercent = -rightSpacing/CGRectGetWidth(rect);
  241. if (playerDisapperaPercent > 1.0) playerDisapperaPercent = 1.0;
  242. if (self.zf_playerDisappearingInScrollView) self.zf_playerDisappearingInScrollView(self.zf_playingIndexPath, playerDisapperaPercent);
  243. }
  244. /// Bottom area
  245. if (rightSpacing <= 0 && rightSpacing > -CGRectGetWidth(rect)/2) {
  246. /// When the player will disappear.
  247. if (self.zf_playerWillDisappearInScrollView) self.zf_playerWillDisappearInScrollView(self.zf_playingIndexPath);
  248. } else if (rightSpacing <= -CGRectGetWidth(rect)) {
  249. /// When the player did disappeared.
  250. if (self.zf_playerDidDisappearInScrollView) self.zf_playerDidDisappearInScrollView(self.zf_playingIndexPath);
  251. } else if (rightSpacing > 0 && rightSpacing <= contentInsetWidth) {
  252. /// Player is appearing.
  253. if (CGRectGetWidth(rect) != 0) {
  254. playerApperaPercent = -(rightSpacing-contentInsetWidth)/CGRectGetWidth(rect);
  255. if (playerApperaPercent > 1.0) playerApperaPercent = 1.0;
  256. if (self.zf_playerAppearingInScrollView) self.zf_playerAppearingInScrollView(self.zf_playingIndexPath, playerApperaPercent);
  257. }
  258. /// In visable area
  259. if (rightSpacing <= contentInsetWidth && rightSpacing > contentInsetWidth-CGRectGetWidth(rect)/2) {
  260. /// When the player will appear.
  261. if (self.zf_playerWillAppearInScrollView) self.zf_playerWillAppearInScrollView(self.zf_playingIndexPath);
  262. } else {
  263. /// When the player did appeared.
  264. if (self.zf_playerDidAppearInScrollView) self.zf_playerDidAppearInScrollView(self.zf_playingIndexPath);
  265. }
  266. }
  267. }
  268. }
  269. /**
  270. Find the playing cell while the scrollDirection is vertical.
  271. */
  272. - (void)_findCorrectCellWhenScrollViewDirectionVertical:(void (^ __nullable)(NSIndexPath *indexPath))handler {
  273. if (!self.zf_shouldAutoPlay) return;
  274. if (self.zf_containerType == ZFPlayerContainerTypeView) return;
  275. NSArray *visiableCells = nil;
  276. NSIndexPath *indexPath = nil;
  277. if ([self _isTableView]) {
  278. UITableView *tableView = (UITableView *)self;
  279. visiableCells = [tableView visibleCells];
  280. // First visible cell indexPath
  281. indexPath = tableView.indexPathsForVisibleRows.firstObject;
  282. if (self.contentOffset.y <= 0 && (!self.zf_playingIndexPath || [indexPath compare:self.zf_playingIndexPath] == NSOrderedSame)) {
  283. UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  284. UIView *playerView = [cell viewWithTag:self.zf_containerViewTag];
  285. if (playerView) {
  286. if (self.zf_scrollViewDidScrollCallback) self.zf_scrollViewDidScrollCallback(indexPath);
  287. if (handler) handler(indexPath);
  288. self.zf_shouldPlayIndexPath = indexPath;
  289. return;
  290. }
  291. }
  292. // Last visible cell indexPath
  293. indexPath = tableView.indexPathsForVisibleRows.lastObject;
  294. if (self.contentOffset.y + self.frame.size.height >= self.contentSize.height && (!self.zf_playingIndexPath || [indexPath compare:self.zf_playingIndexPath] == NSOrderedSame)) {
  295. UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  296. UIView *playerView = [cell viewWithTag:self.zf_containerViewTag];
  297. if (playerView) {
  298. if (self.zf_scrollViewDidScrollCallback) self.zf_scrollViewDidScrollCallback(indexPath);
  299. if (handler) handler(indexPath);
  300. self.zf_shouldPlayIndexPath = indexPath;
  301. return;
  302. }
  303. }
  304. } else if ([self _isCollectionView]) {
  305. UICollectionView *collectionView = (UICollectionView *)self;
  306. visiableCells = [collectionView visibleCells];
  307. NSArray *sortedIndexPaths = [collectionView.indexPathsForVisibleItems sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  308. return [obj1 compare:obj2];
  309. }];
  310. visiableCells = [visiableCells sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  311. NSIndexPath *path1 = (NSIndexPath *)[collectionView indexPathForCell:obj1];
  312. NSIndexPath *path2 = (NSIndexPath *)[collectionView indexPathForCell:obj2];
  313. return [path1 compare:path2];
  314. }];
  315. // First visible cell indexPath
  316. indexPath = sortedIndexPaths.firstObject;
  317. if (self.contentOffset.y <= 0 && (!self.zf_playingIndexPath || [indexPath compare:self.zf_playingIndexPath] == NSOrderedSame)) {
  318. UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  319. UIView *playerView = [cell viewWithTag:self.zf_containerViewTag];
  320. if (playerView) {
  321. if (self.zf_scrollViewDidScrollCallback) self.zf_scrollViewDidScrollCallback(indexPath);
  322. if (handler) handler(indexPath);
  323. self.zf_shouldPlayIndexPath = indexPath;
  324. return;
  325. }
  326. }
  327. // Last visible cell indexPath
  328. indexPath = sortedIndexPaths.lastObject;
  329. if (self.contentOffset.y + self.frame.size.height >= self.contentSize.height && (!self.zf_playingIndexPath || [indexPath compare:self.zf_playingIndexPath] == NSOrderedSame)) {
  330. UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  331. UIView *playerView = [cell viewWithTag:self.zf_containerViewTag];
  332. if (playerView) {
  333. if (self.zf_scrollViewDidScrollCallback) self.zf_scrollViewDidScrollCallback(indexPath);
  334. if (handler) handler(indexPath);
  335. self.zf_shouldPlayIndexPath = indexPath;
  336. return;
  337. }
  338. }
  339. }
  340. NSArray *cells = nil;
  341. if (self.zf_scrollDirection == ZFPlayerScrollDirectionUp) {
  342. cells = visiableCells;
  343. } else {
  344. cells = [visiableCells reverseObjectEnumerator].allObjects;
  345. }
  346. /// Mid line.
  347. CGFloat scrollViewMidY = CGRectGetHeight(self.frame)/2;
  348. /// The final playing indexPath.
  349. __block NSIndexPath *finalIndexPath = nil;
  350. /// The final distance from the center line.
  351. __block CGFloat finalSpace = 0;
  352. @weakify(self)
  353. [cells enumerateObjectsUsingBlock:^(UIView *cell, NSUInteger idx, BOOL * _Nonnull stop) {
  354. @strongify(self)
  355. UIView *playerView = [cell viewWithTag:self.zf_containerViewTag];
  356. if (!playerView) return;
  357. CGRect rect1 = [playerView convertRect:playerView.frame toView:self];
  358. CGRect rect = [self convertRect:rect1 toView:self.superview];
  359. /// playerView top to scrollView top space.
  360. CGFloat topSpacing = CGRectGetMinY(rect) - CGRectGetMinY(self.frame) - CGRectGetMinY(playerView.frame);
  361. /// playerView bottom to scrollView bottom space.
  362. CGFloat bottomSpacing = CGRectGetMaxY(self.frame) - CGRectGetMaxY(rect) + CGRectGetMinY(playerView.frame);
  363. CGFloat centerSpacing = ABS(scrollViewMidY - CGRectGetMidY(rect));
  364. NSIndexPath *indexPath = [self zf_getIndexPathForCell:cell];
  365. /// Play when the video playback section is visible.
  366. if ((topSpacing >= -(1 - self.zf_playerApperaPercent) * CGRectGetHeight(rect)) && (bottomSpacing >= -(1 - self.zf_playerApperaPercent) * CGRectGetHeight(rect))) {
  367. /// If you have a cell that is playing, stop the traversal.
  368. if (self.zf_playingIndexPath) {
  369. indexPath = self.zf_playingIndexPath;
  370. finalIndexPath = indexPath;
  371. *stop = YES;
  372. return;
  373. }
  374. if (!finalIndexPath || centerSpacing < finalSpace) {
  375. finalIndexPath = indexPath;
  376. finalSpace = centerSpacing;
  377. }
  378. }
  379. }];
  380. /// if find the playing indexPath.
  381. if (finalIndexPath) {
  382. if (self.zf_scrollViewDidScrollCallback) self.zf_scrollViewDidScrollCallback(indexPath);
  383. if (handler) handler(finalIndexPath);
  384. }
  385. self.zf_shouldPlayIndexPath = finalIndexPath;
  386. }
  387. /**
  388. Find the playing cell while the scrollDirection is horizontal.
  389. */
  390. - (void)_findCorrectCellWhenScrollViewDirectionHorizontal:(void (^ __nullable)(NSIndexPath *indexPath))handler {
  391. if (!self.zf_shouldAutoPlay) return;
  392. if (self.zf_containerType == ZFPlayerContainerTypeView) return;
  393. NSArray *visiableCells = nil;
  394. NSIndexPath *indexPath = nil;
  395. if ([self _isTableView]) {
  396. UITableView *tableView = (UITableView *)self;
  397. visiableCells = [tableView visibleCells];
  398. // First visible cell indexPath
  399. indexPath = tableView.indexPathsForVisibleRows.firstObject;
  400. if (self.contentOffset.x <= 0 && (!self.zf_playingIndexPath || [indexPath compare:self.zf_playingIndexPath] == NSOrderedSame)) {
  401. UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  402. UIView *playerView = [cell viewWithTag:self.zf_containerViewTag];
  403. if (playerView) {
  404. if (self.zf_scrollViewDidScrollCallback) self.zf_scrollViewDidScrollCallback(indexPath);
  405. if (handler) handler(indexPath);
  406. self.zf_shouldPlayIndexPath = indexPath;
  407. return;
  408. }
  409. }
  410. // Last visible cell indexPath
  411. indexPath = tableView.indexPathsForVisibleRows.lastObject;
  412. if (self.contentOffset.x + self.frame.size.width >= self.contentSize.width && (!self.zf_playingIndexPath || [indexPath compare:self.zf_playingIndexPath] == NSOrderedSame)) {
  413. UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  414. UIView *playerView = [cell viewWithTag:self.zf_containerViewTag];
  415. if (playerView) {
  416. if (self.zf_scrollViewDidScrollCallback) self.zf_scrollViewDidScrollCallback(indexPath);
  417. if (handler) handler(indexPath);
  418. self.zf_shouldPlayIndexPath = indexPath;
  419. return;
  420. }
  421. }
  422. } else if ([self _isCollectionView]) {
  423. UICollectionView *collectionView = (UICollectionView *)self;
  424. visiableCells = [collectionView visibleCells];
  425. NSArray *sortedIndexPaths = [collectionView.indexPathsForVisibleItems sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  426. return [obj1 compare:obj2];
  427. }];
  428. visiableCells = [visiableCells sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  429. NSIndexPath *path1 = (NSIndexPath *)[collectionView indexPathForCell:obj1];
  430. NSIndexPath *path2 = (NSIndexPath *)[collectionView indexPathForCell:obj2];
  431. return [path1 compare:path2];
  432. }];
  433. // First visible cell indexPath
  434. indexPath = sortedIndexPaths.firstObject;
  435. if (self.contentOffset.x <= 0 && (!self.zf_playingIndexPath || [indexPath compare:self.zf_playingIndexPath] == NSOrderedSame)) {
  436. UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  437. UIView *playerView = [cell viewWithTag:self.zf_containerViewTag];
  438. if (playerView) {
  439. if (self.zf_scrollViewDidScrollCallback) self.zf_scrollViewDidScrollCallback(indexPath);
  440. if (handler) handler(indexPath);
  441. self.zf_shouldPlayIndexPath = indexPath;
  442. return;
  443. }
  444. }
  445. // Last visible cell indexPath
  446. indexPath = sortedIndexPaths.lastObject;
  447. if (self.contentOffset.x + self.frame.size.width >= self.contentSize.width && (!self.zf_playingIndexPath || [indexPath compare:self.zf_playingIndexPath] == NSOrderedSame)) {
  448. UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  449. UIView *playerView = [cell viewWithTag:self.zf_containerViewTag];
  450. if (playerView) {
  451. if (self.zf_scrollViewDidScrollCallback) self.zf_scrollViewDidScrollCallback(indexPath);
  452. if (handler) handler(indexPath);
  453. self.zf_shouldPlayIndexPath = indexPath;
  454. return;
  455. }
  456. }
  457. }
  458. NSArray *cells = nil;
  459. if (self.zf_scrollDirection == ZFPlayerScrollDirectionUp) {
  460. cells = visiableCells;
  461. } else {
  462. cells = [visiableCells reverseObjectEnumerator].allObjects;
  463. }
  464. /// Mid line.
  465. CGFloat scrollViewMidX = CGRectGetWidth(self.frame)/2;
  466. /// The final playing indexPath.
  467. __block NSIndexPath *finalIndexPath = nil;
  468. /// The final distance from the center line.
  469. __block CGFloat finalSpace = 0;
  470. @weakify(self)
  471. [cells enumerateObjectsUsingBlock:^(UIView *cell, NSUInteger idx, BOOL * _Nonnull stop) {
  472. @strongify(self)
  473. UIView *playerView = [cell viewWithTag:self.zf_containerViewTag];
  474. if (!playerView) return;
  475. CGRect rect1 = [playerView convertRect:playerView.frame toView:self];
  476. CGRect rect = [self convertRect:rect1 toView:self.superview];
  477. /// playerView left to scrollView top space.
  478. CGFloat leftSpacing = CGRectGetMinX(rect) - CGRectGetMinX(self.frame) - CGRectGetMinX(playerView.frame);
  479. /// playerView right to scrollView top space.
  480. CGFloat rightSpacing = CGRectGetMaxX(self.frame) - CGRectGetMaxX(rect) + CGRectGetMinX(playerView.frame);
  481. CGFloat centerSpacing = ABS(scrollViewMidX - CGRectGetMidX(rect));
  482. NSIndexPath *indexPath = [self zf_getIndexPathForCell:cell];
  483. /// Play when the video playback section is visible.
  484. if ((leftSpacing >= -(1 - self.zf_playerApperaPercent) * CGRectGetWidth(rect)) && (rightSpacing >= -(1 - self.zf_playerApperaPercent) * CGRectGetWidth(rect))) {
  485. /// If you have a cell that is playing, stop the traversal.
  486. if (self.zf_playingIndexPath) {
  487. indexPath = self.zf_playingIndexPath;
  488. finalIndexPath = indexPath;
  489. *stop = YES;
  490. return;
  491. }
  492. if (!finalIndexPath || centerSpacing < finalSpace) {
  493. finalIndexPath = indexPath;
  494. finalSpace = centerSpacing;
  495. }
  496. }
  497. }];
  498. /// if find the playing indexPath.
  499. if (finalIndexPath) {
  500. if (self.zf_scrollViewDidScrollCallback) self.zf_scrollViewDidScrollCallback(indexPath);
  501. if (handler) handler(finalIndexPath);
  502. self.zf_shouldPlayIndexPath = finalIndexPath;
  503. }
  504. }
  505. - (BOOL)_isTableView {
  506. return [self isKindOfClass:[UITableView class]];
  507. }
  508. - (BOOL)_isCollectionView {
  509. return [self isKindOfClass:[UICollectionView class]];
  510. }
  511. #pragma mark - public method
  512. - (UIView *)zf_getCellForIndexPath:(NSIndexPath *)indexPath {
  513. if ([self _isTableView]) {
  514. UITableView *tableView = (UITableView *)self;
  515. UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  516. return cell;
  517. } else if ([self _isCollectionView]) {
  518. UICollectionView *collectionView = (UICollectionView *)self;
  519. UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  520. return cell;
  521. }
  522. return nil;
  523. }
  524. - (NSIndexPath *)zf_getIndexPathForCell:(UIView *)cell {
  525. if ([self _isTableView]) {
  526. UITableView *tableView = (UITableView *)self;
  527. NSIndexPath *indexPath = [tableView indexPathForCell:(UITableViewCell *)cell];
  528. return indexPath;
  529. } else if ([self _isCollectionView]) {
  530. UICollectionView *collectionView = (UICollectionView *)self;
  531. NSIndexPath *indexPath = [collectionView indexPathForCell:(UICollectionViewCell *)cell];
  532. return indexPath;
  533. }
  534. return nil;
  535. }
  536. - (void)zf_scrollToRowAtIndexPath:(NSIndexPath *)indexPath completionHandler:(void (^ __nullable)(void))completionHandler {
  537. [self zf_scrollToRowAtIndexPath:indexPath animated:YES completionHandler:completionHandler];
  538. }
  539. - (void)zf_scrollToRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated completionHandler:(void (^ __nullable)(void))completionHandler {
  540. [self zf_scrollToRowAtIndexPath:indexPath animateWithDuration:animated ? 0.4 : 0.0 completionHandler:completionHandler];
  541. }
  542. /// Scroll to indexPath with animations duration.
  543. - (void)zf_scrollToRowAtIndexPath:(NSIndexPath *)indexPath animateWithDuration:(NSTimeInterval)duration completionHandler:(void (^ __nullable)(void))completionHandler {
  544. BOOL animated = duration > 0.0;
  545. if ([self _isTableView]) {
  546. UITableView *tableView = (UITableView *)self;
  547. [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:animated];
  548. } else if ([self _isCollectionView]) {
  549. UICollectionView *collectionView = (UICollectionView *)self;
  550. [collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionTop animated:animated];
  551. }
  552. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  553. if (completionHandler) completionHandler();
  554. });
  555. }
  556. - (void)zf_scrollViewDidEndDecelerating {
  557. BOOL scrollToScrollStop = !self.tracking && !self.dragging && !self.decelerating;
  558. if (scrollToScrollStop) {
  559. [self _scrollViewDidStopScroll];
  560. }
  561. }
  562. - (void)zf_scrollViewDidEndDraggingWillDecelerate:(BOOL)decelerate {
  563. if (!decelerate) {
  564. BOOL dragToDragStop = self.tracking && !self.dragging && !self.decelerating;
  565. if (dragToDragStop) {
  566. [self _scrollViewDidStopScroll];
  567. }
  568. }
  569. }
  570. - (void)zf_scrollViewDidScrollToTop {
  571. [self _scrollViewDidStopScroll];
  572. }
  573. - (void)zf_scrollViewDidScroll {
  574. if (self.zf_scrollViewDirection == ZFPlayerScrollViewDirectionVertical) {
  575. [self _scrollViewScrollingDirectionVertical];
  576. } else {
  577. [self _scrollViewScrollingDirectionHorizontal];
  578. }
  579. }
  580. - (void)zf_scrollViewWillBeginDragging {
  581. [self _scrollViewBeginDragging];
  582. }
  583. #pragma mark - getter
  584. - (ZFPlayerScrollDirection)zf_scrollDirection {
  585. return [objc_getAssociatedObject(self, _cmd) integerValue];
  586. }
  587. - (ZFPlayerScrollViewDirection)zf_scrollViewDirection {
  588. return [objc_getAssociatedObject(self, _cmd) integerValue];
  589. }
  590. - (CGFloat)zf_lastOffsetY {
  591. return [objc_getAssociatedObject(self, _cmd) floatValue];
  592. }
  593. - (CGFloat)zf_lastOffsetX {
  594. return [objc_getAssociatedObject(self, _cmd) floatValue];
  595. }
  596. #pragma mark - setter
  597. - (void)setZf_scrollDirection:(ZFPlayerScrollDirection)zf_scrollDirection {
  598. objc_setAssociatedObject(self, @selector(zf_scrollDirection), @(zf_scrollDirection), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  599. }
  600. - (void)setZf_scrollViewDirection:(ZFPlayerScrollViewDirection)zf_scrollViewDirection {
  601. objc_setAssociatedObject(self, @selector(zf_scrollViewDirection), @(zf_scrollViewDirection), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  602. }
  603. - (void)setZf_lastOffsetY:(CGFloat)zf_lastOffsetY {
  604. objc_setAssociatedObject(self, @selector(zf_lastOffsetY), @(zf_lastOffsetY), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  605. }
  606. - (void)setZf_lastOffsetX:(CGFloat)zf_lastOffsetX {
  607. objc_setAssociatedObject(self, @selector(zf_lastOffsetX), @(zf_lastOffsetX), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  608. }
  609. @end
  610. @implementation UIScrollView (ZFPlayerCannotCalled)
  611. - (void)zf_filterShouldPlayCellWhileScrolling:(void (^ __nullable)(NSIndexPath *indexPath))handler {
  612. if (self.zf_scrollViewDirection == ZFPlayerScrollViewDirectionVertical) {
  613. [self _findCorrectCellWhenScrollViewDirectionVertical:handler];
  614. } else {
  615. [self _findCorrectCellWhenScrollViewDirectionHorizontal:handler];
  616. }
  617. }
  618. - (void)zf_filterShouldPlayCellWhileScrolled:(void (^ __nullable)(NSIndexPath *indexPath))handler {
  619. if (!self.zf_shouldAutoPlay) return;
  620. @weakify(self)
  621. [self zf_filterShouldPlayCellWhileScrolling:^(NSIndexPath *indexPath) {
  622. @strongify(self)
  623. /// 如果当前控制器已经消失,直接return
  624. if (self.zf_viewControllerDisappear) return;
  625. if ([ZFReachabilityManager sharedManager].isReachableViaWWAN && !self.zf_WWANAutoPlay) {
  626. /// 移动网络
  627. self.zf_shouldPlayIndexPath = indexPath;
  628. return;
  629. }
  630. if (handler) handler(indexPath);
  631. self.zf_playingIndexPath = indexPath;
  632. }];
  633. }
  634. #pragma mark - getter
  635. - (void (^)(NSIndexPath * _Nonnull, CGFloat))zf_playerDisappearingInScrollView {
  636. return objc_getAssociatedObject(self, _cmd);
  637. }
  638. - (void (^)(NSIndexPath * _Nonnull, CGFloat))zf_playerAppearingInScrollView {
  639. return objc_getAssociatedObject(self, _cmd);
  640. }
  641. - (void (^)(NSIndexPath * _Nonnull))zf_playerDidAppearInScrollView {
  642. return objc_getAssociatedObject(self, _cmd);
  643. }
  644. - (void (^)(NSIndexPath * _Nonnull))zf_playerWillDisappearInScrollView {
  645. return objc_getAssociatedObject(self, _cmd);
  646. }
  647. - (void (^)(NSIndexPath * _Nonnull))zf_playerWillAppearInScrollView {
  648. return objc_getAssociatedObject(self, _cmd);
  649. }
  650. - (void (^)(NSIndexPath * _Nonnull))zf_playerDidDisappearInScrollView {
  651. return objc_getAssociatedObject(self, _cmd);
  652. }
  653. - (void (^)(NSIndexPath * _Nonnull))zf_scrollViewDidEndScrollingCallback {
  654. return objc_getAssociatedObject(self, _cmd);
  655. }
  656. - (void (^)(NSIndexPath * _Nonnull))zf_scrollViewDidScrollCallback {
  657. return objc_getAssociatedObject(self, _cmd);
  658. }
  659. - (void (^)(NSIndexPath * _Nonnull))zf_playerShouldPlayInScrollView {
  660. return objc_getAssociatedObject(self, _cmd);
  661. }
  662. - (CGFloat)zf_playerApperaPercent {
  663. return [objc_getAssociatedObject(self, _cmd) floatValue];
  664. }
  665. - (CGFloat)zf_playerDisapperaPercent {
  666. return [objc_getAssociatedObject(self, _cmd) floatValue];
  667. }
  668. - (BOOL)zf_viewControllerDisappear {
  669. return [objc_getAssociatedObject(self, _cmd) boolValue];
  670. }
  671. - (BOOL)zf_stopPlay {
  672. return [objc_getAssociatedObject(self, _cmd) boolValue];
  673. }
  674. - (BOOL)zf_stopWhileNotVisible {
  675. return [objc_getAssociatedObject(self, _cmd) boolValue];
  676. }
  677. - (NSIndexPath *)zf_playingIndexPath {
  678. return objc_getAssociatedObject(self, _cmd);
  679. }
  680. - (NSIndexPath *)zf_shouldPlayIndexPath {
  681. return objc_getAssociatedObject(self, _cmd);
  682. }
  683. - (NSInteger)zf_containerViewTag {
  684. return [objc_getAssociatedObject(self, _cmd) integerValue];
  685. }
  686. - (BOOL)zf_isWWANAutoPlay {
  687. return [objc_getAssociatedObject(self, _cmd) boolValue];
  688. }
  689. - (BOOL)zf_shouldAutoPlay {
  690. NSNumber *number = objc_getAssociatedObject(self, _cmd);
  691. if (number) return number.boolValue;
  692. self.zf_shouldAutoPlay = YES;
  693. return YES;
  694. }
  695. - (ZFPlayerContainerType)zf_containerType {
  696. return [objc_getAssociatedObject(self, _cmd) integerValue];
  697. }
  698. - (UIView *)zf_containerView {
  699. return objc_getAssociatedObject(self, _cmd);
  700. }
  701. #pragma mark - setter
  702. - (void)setZf_playerDisappearingInScrollView:(void (^)(NSIndexPath * _Nonnull, CGFloat))zf_playerDisappearingInScrollView {
  703. objc_setAssociatedObject(self, @selector(zf_playerDisappearingInScrollView), zf_playerDisappearingInScrollView, OBJC_ASSOCIATION_COPY_NONATOMIC);
  704. }
  705. - (void)setZf_playerAppearingInScrollView:(void (^)(NSIndexPath * _Nonnull, CGFloat))zf_playerAppearingInScrollView {
  706. objc_setAssociatedObject(self, @selector(zf_playerAppearingInScrollView), zf_playerAppearingInScrollView, OBJC_ASSOCIATION_COPY_NONATOMIC);
  707. }
  708. - (void)setZf_playerDidAppearInScrollView:(void (^)(NSIndexPath * _Nonnull))zf_playerDidAppearInScrollView {
  709. objc_setAssociatedObject(self, @selector(zf_playerDidAppearInScrollView), zf_playerDidAppearInScrollView, OBJC_ASSOCIATION_COPY_NONATOMIC);
  710. }
  711. - (void)setZf_playerWillDisappearInScrollView:(void (^)(NSIndexPath * _Nonnull))zf_playerWillDisappearInScrollView {
  712. objc_setAssociatedObject(self, @selector(zf_playerWillDisappearInScrollView), zf_playerWillDisappearInScrollView, OBJC_ASSOCIATION_COPY_NONATOMIC);
  713. }
  714. - (void)setZf_playerWillAppearInScrollView:(void (^)(NSIndexPath * _Nonnull))zf_playerWillAppearInScrollView {
  715. objc_setAssociatedObject(self, @selector(zf_playerWillAppearInScrollView), zf_playerWillAppearInScrollView, OBJC_ASSOCIATION_COPY_NONATOMIC);
  716. }
  717. - (void)setZf_playerDidDisappearInScrollView:(void (^)(NSIndexPath * _Nonnull))zf_playerDidDisappearInScrollView {
  718. objc_setAssociatedObject(self, @selector(zf_playerDidDisappearInScrollView), zf_playerDidDisappearInScrollView, OBJC_ASSOCIATION_COPY_NONATOMIC);
  719. }
  720. - (void)setZf_scrollViewDidEndScrollingCallback:(void (^)(NSIndexPath * _Nonnull))zf_scrollViewDidEndScrollingCallback {
  721. objc_setAssociatedObject(self, @selector(zf_scrollViewDidEndScrollingCallback), zf_scrollViewDidEndScrollingCallback, OBJC_ASSOCIATION_COPY_NONATOMIC);
  722. }
  723. - (void)setZf_scrollViewDidScrollCallback:(void (^)(NSIndexPath * _Nonnull))zf_scrollViewDidScrollCallback {
  724. objc_setAssociatedObject(self, @selector(zf_scrollViewDidScrollCallback), zf_scrollViewDidScrollCallback, OBJC_ASSOCIATION_COPY_NONATOMIC);
  725. }
  726. - (void)setZf_playerShouldPlayInScrollView:(void (^)(NSIndexPath * _Nonnull))zf_playerShouldPlayInScrollView {
  727. objc_setAssociatedObject(self, @selector(zf_playerShouldPlayInScrollView), zf_playerShouldPlayInScrollView, OBJC_ASSOCIATION_COPY_NONATOMIC);
  728. }
  729. - (void)setZf_playerApperaPercent:(CGFloat)zf_playerApperaPercent {
  730. objc_setAssociatedObject(self, @selector(zf_playerApperaPercent), @(zf_playerApperaPercent), OBJC_ASSOCIATION_COPY_NONATOMIC);
  731. }
  732. - (void)setZf_playerDisapperaPercent:(CGFloat)zf_playerDisapperaPercent {
  733. objc_setAssociatedObject(self, @selector(zf_playerDisapperaPercent), @(zf_playerDisapperaPercent), OBJC_ASSOCIATION_COPY_NONATOMIC);
  734. }
  735. - (void)setZf_viewControllerDisappear:(BOOL)zf_viewControllerDisappear {
  736. objc_setAssociatedObject(self, @selector(zf_viewControllerDisappear), @(zf_viewControllerDisappear), OBJC_ASSOCIATION_COPY_NONATOMIC);
  737. }
  738. - (void)setZf_stopPlay:(BOOL)zf_stopPlay {
  739. objc_setAssociatedObject(self, @selector(zf_stopPlay), @(zf_stopPlay), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  740. }
  741. - (void)setZf_stopWhileNotVisible:(BOOL)zf_stopWhileNotVisible {
  742. objc_setAssociatedObject(self, @selector(zf_stopWhileNotVisible), @(zf_stopWhileNotVisible), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  743. }
  744. - (void)setZf_playingIndexPath:(NSIndexPath *)zf_playingIndexPath {
  745. objc_setAssociatedObject(self, @selector(zf_playingIndexPath), zf_playingIndexPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  746. if (zf_playingIndexPath && [zf_playingIndexPath compare:self.zf_shouldPlayIndexPath] != NSOrderedSame) {
  747. self.zf_shouldPlayIndexPath = zf_playingIndexPath;
  748. }
  749. }
  750. - (void)setZf_shouldPlayIndexPath:(NSIndexPath *)zf_shouldPlayIndexPath {
  751. if (self.zf_playerShouldPlayInScrollView) self.zf_playerShouldPlayInScrollView(zf_shouldPlayIndexPath);
  752. if (self.zf_shouldPlayIndexPathCallback) self.zf_shouldPlayIndexPathCallback(zf_shouldPlayIndexPath);
  753. objc_setAssociatedObject(self, @selector(zf_shouldPlayIndexPath), zf_shouldPlayIndexPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  754. }
  755. - (void)setZf_containerViewTag:(NSInteger)zf_containerViewTag {
  756. objc_setAssociatedObject(self, @selector(zf_containerViewTag), @(zf_containerViewTag), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  757. }
  758. - (void)setZf_containerType:(ZFPlayerContainerType)zf_containerType {
  759. objc_setAssociatedObject(self, @selector(zf_containerType), @(zf_containerType), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  760. }
  761. - (void)setZf_containerView:(UIView *)zf_containerView {
  762. objc_setAssociatedObject(self, @selector(zf_containerView), zf_containerView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  763. }
  764. - (void)setZf_shouldAutoPlay:(BOOL)zf_shouldAutoPlay {
  765. objc_setAssociatedObject(self, @selector(zf_shouldAutoPlay), @(zf_shouldAutoPlay), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  766. }
  767. - (void)setZf_WWANAutoPlay:(BOOL)zf_WWANAutoPlay {
  768. objc_setAssociatedObject(self, @selector(zf_isWWANAutoPlay), @(zf_WWANAutoPlay), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  769. }
  770. @end
  771. @implementation UIScrollView (ZFPlayerDeprecated)
  772. #pragma mark - getter
  773. - (void (^)(NSIndexPath * _Nonnull))zf_scrollViewDidStopScrollCallback {
  774. return objc_getAssociatedObject(self, _cmd);
  775. }
  776. - (void (^)(NSIndexPath * _Nonnull))zf_shouldPlayIndexPathCallback {
  777. return objc_getAssociatedObject(self, _cmd);
  778. }
  779. #pragma mark - setter
  780. - (void)setZf_scrollViewDidStopScrollCallback:(void (^)(NSIndexPath * _Nonnull))zf_scrollViewDidStopScrollCallback {
  781. objc_setAssociatedObject(self, @selector(zf_scrollViewDidStopScrollCallback), zf_scrollViewDidStopScrollCallback, OBJC_ASSOCIATION_COPY_NONATOMIC);
  782. }
  783. - (void)setZf_shouldPlayIndexPathCallback:(void (^)(NSIndexPath * _Nonnull))zf_shouldPlayIndexPathCallback {
  784. objc_setAssociatedObject(self, @selector(zf_shouldPlayIndexPathCallback), zf_shouldPlayIndexPathCallback, OBJC_ASSOCIATION_COPY_NONATOMIC);
  785. }
  786. @end
  787. #pragma clang diagnostic pop