ios 視頻列表處理---分解ZFPlayer


1.視頻播放器添加到containerView的機制與一個普通播放器頁面的不同

普通視頻播放頁面可以直接添加一個播放器,按照正常邏輯播放、暫停、切換等操作,而視頻列表的做法是

用戶觸發播放動作

當點擊一個cell上的播放按鈕時,首先判斷當前是否有其他cell在播放視頻,有則停止播放並移除播放器,

 

反之,會判斷是否存在有效的承載控件,即containerView,有的話就addplayer,然后通過給assetURL賦值然后啟動播放。

 

 

 

2.視頻播放器的移除與視頻的停止播放觸發機制(當正在播放的cell上的播放器離開屏幕有效區域時)

 

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) {
            
            return cell;
        }
    }
    
    return nil;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM