场景:一个滚动的视频列表,类似抖音,利用UICollctionView实现当cell到达一定位置就播放该cell视频,cell滑出就停止播放。
实现思路:其中MyPlayViewCell为cell的类,playingCell为记录当前播放的cell
1.列表为UICollctionView,判断当前cell滚动位置并播放:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSIndexPath *indexPathPlay;
if (scrollView.contentOffset.y <= 0) {
indexPathPlay = [NSIndexPath indexPathForItem:0 inSection:0];
}else{
indexPathPlay = [self.collectionView indexPathForItemAtPoint:CGPointMake(0, scrollView.contentOffset.y + self.collectionView.frame.size.height/2)];//这里取的是collctionview高程中心点
}
MyPlayViewCell *cell = (MyPlayViewCell *)[self.collectionView cellForItemAtIndexPath:indexPathPlay];
if ([cell isKindOfClass:[MyPlayView