iOS仿抖音—上下滑動播放視頻

iOS仿抖音短視頻

iOS仿抖音—左右滑動切換效果
iOS仿抖音—上下滑動播放視頻
iOS仿抖音—評論視圖滑動消失
iOS仿抖音—加載點贊動畫效果
iOS仿抖音—播放視圖滑動隱藏
首先看下效果圖

圖做的不太好,將就看吧

2018.12.12 添加了tabbar,數(shù)據(jù)寫死到了本地(接口過一段時間就會訪問沒數(shù)據(jù))


dy_tabbar.gif

前言

上一篇文章仿寫了抖音的左右滑動效果-iOS之仿抖音左右滑動效果,有興趣的可以去GKNavigationBarViewController的demo中查看。
這篇文章主要是對抖音的上下滑動及視頻播放功能做介紹。其中上下滑動用的是UIScrollview,包含3個子視圖。視頻播放用的是TX的獨立播放器TXLiteAVSDK_Player,可實現(xiàn)音視頻的點播、直播等功能。

demo中的視頻是通過抓包獲取的百度的伙拍小視頻,僅供學習使用。

說明

1、上下滑動切換視圖實現(xiàn)

主要是在UIScrollview的代理中做處理,且對滑動到第一個和最后一個時分開處理,看下代碼

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // 小于等于三個,不用處理
    if (self.videos.count <= 3) return;
    
    // 上滑到第一個
    if (self.index == 0 && scrollView.contentOffset.y <= SCREEN_HEIGHT) {
        return;
    }
    // 下滑到最后一個
    if (self.index == self.videos.count - 1 && scrollView.contentOffset.y > SCREEN_HEIGHT) {
        return;
    }
    
    // 判斷是從中間視圖上滑還是下滑
    if (scrollView.contentOffset.y >= 2 * SCREEN_HEIGHT) {  // 上滑
        [self.player removeVideo];  // 在這里移除播放,解決閃動的bug
        if (self.index == 0) {
            self.index += 2;
            
            scrollView.contentOffset = CGPointMake(0, SCREEN_HEIGHT);
            
            self.topView.model = self.ctrView.model;
            self.ctrView.model = self.btmView.model;
            
        }else {
            self.index += 1;
            
            if (self.index == self.videos.count - 1) {
                self.ctrView.model = self.videos[self.index - 1];
            }else {
                scrollView.contentOffset = CGPointMake(0, SCREEN_HEIGHT);
                
                self.topView.model = self.ctrView.model;
                self.ctrView.model = self.btmView.model;
            }
        }
        if (self.index < self.videos.count - 1) {
            self.btmView.model = self.videos[self.index + 1];
        }
    }else if (scrollView.contentOffset.y <= 0) { // 下滑
        [self.player removeVideo];  // 在這里移除播放,解決閃動的bug
        if (self.index == 1) {
            self.topView.model = self.videos[self.index - 1];
            self.ctrView.model = self.videos[self.index];
            self.btmView.model = self.videos[self.index + 1];
            self.index -= 1;
        }else {
            if (self.index == self.videos.count - 1) {
                self.index -= 2;
            }else {
                self.index -= 1;
            }
            scrollView.contentOffset = CGPointMake(0, SCREEN_HEIGHT);
            
            self.btmView.model = self.ctrView.model;
            self.ctrView.model = self.topView.model;
            
            if (self.index > 0) {
                self.topView.model = self.videos[self.index - 1];
            }
        }
    }
}

滑動結(jié)束后開始播放

// 結(jié)束滾動后開始播放
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y == 0) {
        if (self.currentPlayId == self.topView.model.post_id) return;
        [self playVideoFrom:self.topView];
    }else if (scrollView.contentOffset.y == SCREEN_HEIGHT) {
        if (self.currentPlayId == self.ctrView.model.post_id) return;
        [self playVideoFrom:self.ctrView];
    }else if (scrollView.contentOffset.y == 2 * SCREEN_HEIGHT) {
        if (self.currentPlayId == self.btmView.model.post_id) return;
        [self playVideoFrom:self.btmView];
    }
    
    if (self.isPushed) return;
    
    // 當只剩最后兩個的時候,獲取新數(shù)據(jù)
    if (self.currentPlayIndex == self.videos.count - 2) {
        [self.viewModel refreshNewListWithSuccess:^(NSArray * _Nonnull list) {
            [self.videos addObjectsFromArray:list];
        } failure:^(NSError * _Nonnull error) {
            NSLog(@"%@", error);
        }];
    }
}

上下滑動的處理基本就這些,如果有不懂的可以下載demo查看。

2、播放器封裝

這里封裝的播放器用的是騰訊的視頻點播TXVodPlayer。
創(chuàng)建播放器

- (TXVodPlayer *)player {
    if (!_player) {
        [TXLiveBase setLogLevel:LOGLEVEL_NULL];
        [TXLiveBase setConsoleEnabled:NO];
        
        _player = [TXVodPlayer new];
        _player.vodDelegate = self;
        _player.loop = YES; // 開啟循環(huán)播放功能
    }
    return _player;
}

由于是多個視頻切換播放,所以最好只用一個播放器,因此在切換視圖后,需要播放器切換播放視圖和播放地址,所以提供了下面的方法。

- (void)playVideoWithView:(UIView *)playView url:(NSString *)url {
    // 設置播放視圖
    [self.player setupVideoWidget:playView insertIndex:0];
    
    // 準備播放
    [self playerStatusChanged:GKDYVideoPlayerStatusPrepared];
    
    // 開始播放
    if ([self.player startPlay:url] == 0) {
        // 這里可加入緩沖視圖
    }else {
        [self playerStatusChanged:GKDYVideoPlayerStatusError];
    }
}

播放器狀態(tài)監(jiān)聽,可獲取播放狀態(tài)及進度等

- (void)onPlayEvent:(TXVodPlayer *)player event:(int)EvtID withParam:(NSDictionary *)param {
    switch (EvtID) {
        case PLAY_EVT_PLAY_LOADING:{    // loading
            if (self.status == GKDYVideoPlayerStatusPaused) {
                [self playerStatusChanged:GKDYVideoPlayerStatusPaused];
            }else {
                [self playerStatusChanged:GKDYVideoPlayerStatusLoading];
            }
        }
            break;
        case PLAY_EVT_PLAY_BEGIN:{    // 開始播放
            [self playerStatusChanged:GKDYVideoPlayerStatusPlaying];
        }
            break;
        case PLAY_EVT_PLAY_END:{    // 播放結(jié)束
            [self playerStatusChanged:GKDYVideoPlayerStatusEnded];
        }
            break;
        case PLAY_ERR_NET_DISCONNECT:{    // 失敗,多次重連無效
            [self playerStatusChanged:GKDYVideoPlayerStatusError];
        }
            break;
        case PLAY_EVT_PLAY_PROGRESS:{    // 進度
            if (self.status == GKDYVideoPlayerStatusPlaying) {
                self.duration = [param[EVT_PLAY_DURATION] floatValue];
                
                float currTime = [param[EVT_PLAY_PROGRESS] floatValue];
                
                float progress = self.duration == 0 ? 0 : currTime / self.duration;
                
                // 處理播放結(jié)束時,進度不更新問題
                if (progress >= 0.95) progress = 1.0f;
                
                //                float buffTime = [param[EVT_PLAYABLE_DURATION] floatValue];
                //                float burrProgress = self.duration == 0 ? 0 : buffTime / self.duration;
                
                if ([self.delegate respondsToSelector:@selector(player:currentTime:totalTime:progress:)]) {
                    [self.delegate player:self currentTime:currTime totalTime:self.duration progress:progress];
                }
            }
        }
            break;
            
        default:
            break;
    }
}

內(nèi)容比較多,如果想要具體了解,還需要下載代碼查看。

最后

demo的地址:GKDYVideo,由于github的限制,播放器無法上傳,所以需要下載下來后pod install即可。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,231評論 3 119
  • 自從婦女節(jié)被定為國際節(jié)日后,仿佛所有女性都有了被呵護寵愛的權(quán)利,更加自然地接受社會對女性的關愛,尊重,理解;充滿了...
    小半啊閱讀 197評論 0 2
  • 三月的北方 溫柔的春風里含著沙塵 嬌艷的陽光下泛著清冷 枝頭沉甸的粉白 裝點著春天的故事 北方的春日 似北方漢子的...
    劉芷睿閱讀 265評論 0 0
  • 許是一頓火鍋加燒烤鬧的,昨晚臨睡前頭痛不止,吃了藥也無濟于事,最終伴隨著頭痛昏昏沉沉睡過去。 我是極易做夢的人,昨...
    白白一閱讀 229評論 2 0
  • 兒子,明天你就要出發(fā)去參加為期7天并且遠在北京的夏令營了。七天對你來說可能很短,短到結(jié)營的時候和小伙伴還戀戀不舍,...
    深香默默閱讀 303評論 0 0

友情鏈接更多精彩內(nèi)容