直播問題交流可加群 379258188
備注簡(jiǎn)書
在這里我是使用了兩個(gè)UIViewController
,
playerViewController
負(fù)責(zé)播放器相關(guān)的配置以及代理
playerChatViewController
負(fù)責(zé)聊天室,禮物展示以及彈幕的視圖
使用兩個(gè)控制器,可以分離直播間的邏輯,視圖層次更加清晰
添加子控制器
@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;
}
手勢(shì)邏輯
-
playerChatViewController
添加手勢(shì),負(fù)責(zé)往右滑動(dòng)
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [[UIColor greenColor]colorWithAlphaComponent:0.5];
// 拖動(dòng)手勢(shì)
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureView:)];
[self.view addGestureRecognizer:pan];
}
滑動(dòng)到臨界點(diǎn)(自己隨便寫)的時(shí)候,設(shè)定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){ // 臨界點(diǎn)
[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
添加手勢(shì),負(fù)責(zé)chatVc的View往左滑動(dòng)
// 拖動(dòng)手勢(shì)
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureView:)];
[self.view addGestureRecognizer:pan];
//手勢(shì)\清屏動(dòng)畫
- (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){ //臨界點(diǎn)
[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;
}
}