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

你可能感兴趣的:(iOS重拾直播系列-直播间左右滑动清屏)