iOS重拾直播系列-直播間左右滑動清屏

直播問題交流可加群 379258188 備注簡書

在這里我是使用了兩個UIViewController,

  • playerViewController

負責播放器相關的配置以及代理

  • playerChatViewController

負責聊天室,禮物展示以及彈幕的視圖

使用兩個控制器,可以分離直播間的邏輯,視圖層次更加清晰

添加子控制器


    @property (nonatomic, strong) JNPlayerChatViewController *chatVc;
    
    // addChildViewController
    [self addChildViewController:self.chatVc];
    [self.view addSubview:self.chatVc.view];
    self.chatVc.view.frame = self.view.bounds;
    
    // 懶加載控制器
    - (JNPlayerChatViewController *)chatVc {
    if (_chatVc == nil) {
        _chatVc = [[JNPlayerChatViewController alloc]init];
    }
    return _chatVc;
    }

手勢邏輯

  • playerChatViewController添加手勢,負責往右滑動
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [[UIColor greenColor]colorWithAlphaComponent:0.5];
    // 拖動手勢
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureView:)];
    [self.view addGestureRecognizer:pan];
}

滑動到臨界點(自己隨便寫)的時候,設定playerChatViewController的view的x為屏幕寬度

- (void)panGestureView:(UIPanGestureRecognizer*)pan
{
    
    CGPoint point = [pan translationInView:self.view];
    if (pan.state == UIGestureRecognizerStateChanged){
        if (point.x <= 0 )  return;
        self.view.left = point.x;
    }
    
    if (pan.state == UIGestureRecognizerStateEnded) {
        if (point.x <  self.view.width/6){ // 臨界點
            [UIView animateWithDuration:0.3 animations:^{
                self.view.left = 0;
            }];
        }else{
            [UIView animateWithDuration:0.3 animations:^{
                self.view.left = self.view.width;
            }completion:^(BOOL finished) {
                
            }];
        }
    }
}
  • playerViewController添加手勢,負責chatVc的View往左滑動
    // 拖動手勢
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureView:)];
    [self.view addGestureRecognizer:pan];
//手勢\清屏動畫
- (void)panGestureView:(UIPanGestureRecognizer*)pan {
    
    
    CGPoint point = [pan translationInView:pan.view];
    switch (pan.state) {
        case UIGestureRecognizerStateBegan:
            break;
        case UIGestureRecognizerStateChanged:
        {
            self.chatVc.view.left = self.view.width + point.x;
            break;
        }
        case UIGestureRecognizerStateEnded:
        {
            if  (point.x > -kScreenWidth/4){ //臨界點
                [UIView animateWithDuration:0.3 animations:^{
                    self.chatVc.view.left = self.view.width;
                    
                }completion:^(BOOL finished) {
                    
                }];
            }else{
                
                [UIView animateWithDuration:0.3 animations:^{
                    self.chatVc.view.left = 0;
                }completion:^(BOOL finished) {
                    
                }];
            }
        }
            break;
        default:
            break;
    }
}

本文demo https://github.com/TsuiOS/HsuLive

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網絡請求組件 FMDB本地數據庫組件 SD...
    陽明AGI閱讀 16,018評論 3 119
  • “一條大河波浪寬,風吹稻花香兩岸,我家就在岸上住,聽慣了路邊的號子,看慣了船上的白帆……” 祖母過世四年了,如今我...
    Fourteen吳鳳婷閱讀 1,517評論 0 1
  • 1.這節課最重要最深刻的印響? 算我們的生活費以及這一年的消費 2.為什么這三個點對你來說重要或印象深刻? 感覺是...
    facertl閱讀 207評論 0 2
  • 今晚回家的早,陪著老媽看了一會《老男孩》,不了解前后的劇情,只曉得了劉燁演了一位機長。看到了穿著機長制服的他,就想...
    錦瑟年華999閱讀 169評論 0 0
  • 久處不厭,閑談不厭 不敷衍,不怠慢 喜歡著你的喜歡 陪伴著你的陪伴 有著彼此最深的眷戀 不管世事變遷風雨考驗 總是...
    純白色襯衫閱讀 318評論 0 0