ios 视频列表处理---分解ZFPlayer

摘要:
ifreturn;//如果当前没有播放的cell,就无需继续执行以下逻辑代码。

1.视频播放器添加到containerView的机制与一个普通播放器页面的不同

普通视频播放页面可以直接添加一个播放器,按照正常逻辑播放、暂停、切换等操作,而视频列表的做法是

用户触发播放动作

ios 视频列表处理---分解ZFPlayer第1张

当点击一个cell上的播放按钮时,首先判断当前是否有其他cell在播放视频,有则停止播放并移除播放器,

ios 视频列表处理---分解ZFPlayer第2张

反之,会判断是否存在有效的承载控件,即containerView,有的话就addplayer,然后通过给assetURL赋值然后启动播放。

ios 视频列表处理---分解ZFPlayer第3张

ios 视频列表处理---分解ZFPlayer第4张

2.视频播放器的移除与视频的停止播放触发机制(当正在播放的cell上的播放器离开屏幕有效区域时)

ios 视频列表处理---分解ZFPlayer第5张

3.如何判断当前播放视频的cell的相对位移

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    /*如果用户一旦接触scrollview就返回YES,有可能还没有开始拖动
    @property(nonatomic,readonly,getter=isTracking)     BOOL tracking;
     如果用户已经开始拖动就返回YES,
    @property(nonatomic,readonly,getter=isDragging)     BOOL dragging;
     如果用户没有在拖动(手指没有接触scrollview)就返回YES,但是scrollview仍然在惯性滑动
    @property(nonatomic,readonly,getter=isDecelerating) BOOL decelerating;
     */BOOL scrollToScrollStop = !self.tableView.isTracking && !self.tableView.isDragging && !self.tableView.isDecelerating;
    if(scrollToScrollStop) {
        [self _scrollViewDidStopScroll];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
    if (!decelerate) {
        BOOL dragToDragStop = !self.tableView.isTracking && !self.tableView.isDragging && !self.tableView.isDecelerating;
        if(dragToDragStop) {
            [self _scrollViewDidStopScroll];
        }
    }
}

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
   
    [self _scrollViewDidStopScroll];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
   
    [self _scrollViewScrolling];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
   
    [self _scrollViewBeginDragging];
}



#pragma mark - helper

- (void)_scrollViewDidStopScroll {
   
    NSLog(@"tableview已经停止滚动");
}

- (void)_scrollViewBeginDragging {
    self.zf_lastOffsetY =self.tableView.contentOffset.y;
}

- (void)_scrollViewScrolling {
    CGFloat offsetY =self.tableView.contentOffset.y;
    self.zf_scrollDerection = (offsetY - self.zf_lastOffsetY > 0) ?ZFPlayerScrollDerectionUp : ZFPlayerScrollDerectionDown;
    self.zf_lastOffsetY =offsetY;
    
    NSLog(@"%@======self.tableView.contentOffset.y %.0f",self.zf_scrollDerection == ZFPlayerScrollDerectionUp ? @"向上滑动" :@"向下滑动",offsetY);
    
    //当tablview已经无法正常向下滑动,此时如果一直向下拖动tableview,就无需继续执行以下逻辑代码。
    if (self.tableView.contentOffset.y < 0) return;
    
    //如果当前没有播放的cell,就无需继续执行以下逻辑代码。
    if (!self.zf_playingIndexPath) return;
    
    UIView *cell =[self zf_getCellForIndexPath:self.zf_playingIndexPath];
    if (!cell) {
        NSLog(@"没有正在播放视频的cell");
        return;
    }
    UIView *playerView = [cell viewWithTag:1000];
    CGRect rect =[playerView convertRect:playerView.frame toView:self.view];
    
    NSLog(@"把containerView转换rect到VC.view上,与tableview同级");
    
    CGFloat topSpacing = CGRectGetMinY(rect) - CGRectGetMinY(self.tableView.frame) - CGRectGetMinY(playerView.frame) -self.tableView.contentInset.top;
    NSLog(@"当前播放的View距离Tableview<上>边界距离(frame高度):%f",topSpacing);
    
    CGFloat bottomSpacing = CGRectGetMaxY(self.tableView.frame) - CGRectGetMaxY(rect) + CGRectGetMinY(playerView.frame) -self.tableView.contentInset.bottom;
    NSLog(@"当前播放的View距离Tableview<下>边界距离(frame高度):%f",bottomSpacing);
    
    CGFloat contentInsetHeight = CGRectGetMaxY(self.tableView.frame) - CGRectGetMinY(self.tableView.frame) - self.tableView.contentInset.top -self.tableView.contentInset.bottom;
    NSLog(@"当前tableview的内容高度:%f",contentInsetHeight);
    
    CGFloat playerDisapperaPercent = 0;
    CGFloat playerApperaPercent = 0;
    
    //向上滑动
    if (self.zf_scrollDerection == ZFPlayerScrollDerectionUp) { ///Scroll up
        ///Player is disappearing.
        /*场景分析:
         当前播放器位于屏幕中间,向上滑动此时尚未滑出屏幕前 topSpacing-正数 playerDisapperaPercent-负数,
         一旦播放器上边界滑出屏幕playerDisapperaPercent-> 正数并逐步大于1.0,
         此时已经呈现播放器正逐步离开当前屏幕有效区域
         */
        if (topSpacing <= 0 && CGRectGetHeight(rect) != 0) {
            playerDisapperaPercent = -topSpacing/CGRectGetHeight(rect);
            if (playerDisapperaPercent > 1.0) playerDisapperaPercent = 1.0;
            NSLog(@"当前播放视频的cell正在离开当前屏幕有效播放区域。。。。。。");
        }
        
        ///Top area
        if (topSpacing <= 0 && topSpacing > -CGRectGetHeight(rect)/2) {

            NSLog(@"当前播放视频的cell《即将离开》当前屏幕有效播放区域。。。。。。");

        } else if (topSpacing <= -CGRectGetHeight(rect)) {

            NSLog(@"当前播放视频的cell《已经离开》当前屏幕有效播放区域。。。。。。");

        } else if (topSpacing > 0 && topSpacing <=contentInsetHeight) {

            if (CGRectGetHeight(rect) != 0) {
                playerApperaPercent = -(topSpacing-contentInsetHeight)/CGRectGetHeight(rect);
                if (playerApperaPercent > 1.0) playerApperaPercent = 1.0;
                NSLog(@"当前播放视频的cell在当前屏幕有效播放区域上持续出现。。。。。。");
                
            }

            if (topSpacing <= contentInsetHeight && topSpacing > contentInsetHeight-CGRectGetHeight(rect)/2) {

                NSLog(@"当前播放视频的cell《即将出现》在当前屏幕有效播放区域。。。。。。");

            } else{
                NSLog(@"当前播放视频的cell《已经出现》在当前屏幕有效播放区域。。。。。。");
            }
        }
        
    } else if (self.zf_scrollDerection == ZFPlayerScrollDerectionDown) { ///向下滑动

        if (bottomSpacing <= 0 && CGRectGetHeight(rect) != 0) {
            playerDisapperaPercent = -bottomSpacing/CGRectGetHeight(rect);
            if (playerDisapperaPercent > 1.0) playerDisapperaPercent = 1.0;
            NSLog(@"当前播放视频的cell正在离开当前屏幕有效播放区域。。。。。。");
        }

        if (bottomSpacing <= 0 && bottomSpacing > -CGRectGetHeight(rect)/2) {

            NSLog(@"当前播放视频的cell《即将离开》当前屏幕有效播放区域。。。。。。");

        } else if (bottomSpacing <= -CGRectGetHeight(rect)) {
            NSLog(@"当前播放视频的cell《已经离开》当前屏幕有效播放区域。。。。。。");

        } else if (bottomSpacing > 0 && bottomSpacing <=contentInsetHeight) {

            if (CGRectGetHeight(rect) != 0) {
                playerApperaPercent = -(bottomSpacing-contentInsetHeight)/CGRectGetHeight(rect);
                if (playerApperaPercent > 1.0) playerApperaPercent = 1.0;

                NSLog(@"当前播放视频的cell在当前屏幕有效播放区域上持续出现。。。。。。");
            }

            if (bottomSpacing <= contentInsetHeight && bottomSpacing > contentInsetHeight-CGRectGetHeight(rect)/2) {

                NSLog(@"当前播放视频的cell《即将出现》在当前屏幕有效播放区域。。。。。。");
            } else{

                NSLog(@"当前播放视频的cell《已经出现》在当前屏幕有效播放区域。。。。。。");
            }
        }
    }
}

- (UIView *)zf_getCellForIndexPath:(NSIndexPath *)indexPath {
    if(indexPath) {
        
        UITableViewCell *cell =[self.tableView cellForRowAtIndexPath:indexPath];
        if(cell) {
            
            returncell;
        }
    }
    
    returnnil;
}

免责声明:文章转载自《ios 视频列表处理---分解ZFPlayer》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Vue.js自定义标签属性并获取属性,及绑定img的src属性的坑查找文献的BibTex下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

UITableView总结

性能优化 /** * 每当有一个cell要进入视野范围内,就会调用一次 */ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"wine...

iOS UI-文本视图(UITextView)

1 #import "ViewController.h" 2 3 @interface ViewController ()<UITextViewDelegate> 4 5 @property (strong, nonatomic) UITextView *textView; 6 7 @end 8 9 @implement...

入门者初试 Cell(华表)结合C#的应用

本次内容主要包括1.Cell(华表)与VS.NET中常用报表 的 对比参照2.简单的Cell(华表)在C#中的示例因工作需要,现开始接触Cell(华表),用该国产报表软件进行报表开发。和水晶报表及ReportServices等可以在服务器端进行操作的报表不同,Cell(华表)是需要JavaScript或VBScript在客户端进行操作。 Cell对照VS自...

修改easyui默认datagrid的表格内字体大小,样式

如果项目中全局都要修改成某个样式, 找到easyui.css,修改easyui datagrid的表格字体样式,font-size是字体大小。 .datagrid-cell, .datagrid-cell-group, .datagrid-header-rownumber, .datagrid-cell-rownumber {margin:0;paddi...

JAVA poi设置单元格背景颜色

import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.user...

解读eXtremeComponents代码结构--转载

原文地址:http://blog.csdn.net/lark3/article/details/1937466 大致整理了去年写的东西,罗列如下: ec是一系列提供高级显示的开源JSP定制标签,当前的包含的组件为eXtremeTable,用于以表形式显示数据。ec现在的版本是1.0.1,由Jeff Johnston开发的,网址:http://www.ex...