SJVideoPlayer控制器实现短视频列表心路历程

前言:从遇到的问题着手,阐述解决方案。

问题一:如何实现列表横屏?

该页面VC需要支持横屏

- (BOOL)prefersHomeIndicatorAutoHidden {
    return YES;
}

- (BOOL)shouldAutorotate {
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    
    return UIInterfaceOrientationMaskLandscapeRight|UIInterfaceOrientationMaskPortrait;
}

整个App需要支持横屏

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
   return UIInterfaceOrientationMaskAll;
}

手动切换横竖屏

if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget: [UIDevice currentDevice]];
        int val = orientation;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }

添加重力感应横竖屏

发送通知Appdelegate 支持

横竖屏切换的时候调用方法

//iOS8旋转动作的具体执行
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator {
    [super viewWillTransitionToSize:size withTransitionCoordinator: coordinator];
    // 监察者将执行: 1.旋转前的动作  2.旋转后的动作(completion)
    [coordinator animateAlongsideTransition: ^(id context)
     {
       //记录当前
        
        //回主线程 刷新UI
        dispatch_async(dispatch_get_main_queue(), ^{
           
        });
       
        
     } completion: ^(id context) {
        //支持键盘
         [[IQKeyboardManager sharedManager] setEnable:NO];
//         [IQKeyboardManager sharedManager].shouldShowToolbarPlaceholder = NO;
         [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = NO;
     }];
}

问题二:如何实现在table view播放视频?

_player.URLAsset = [[SJVideoPlayerURLAsset alloc] initWithURL:[NSURL URLWithString:item.videoUrl] playModel:[SJPlayModel playModelWithTableView:self.mTableView indexPath:indexPath superviewSelector:NSSelectorFromString(@"容器实例的名字")]];

问题三:如何实现竖屏和横屏的UI切换?

思录:竖屏一个view,横屏一个view。两个view都展示在cell中 在cell初始化的时候就将横竖视图添加上竖屏view显示,然后将当前VC横竖屏状态传给cell中。来回切换两个视图展示。

问题四:如何解决横屏干扰竖屏键盘弹出高度不正确?

 //支持键盘
         [[IQKeyboardManager sharedManager] setEnable:NO];
//         [IQKeyboardManager sharedManager].shouldShowToolbarPlaceholder = NO;
         [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = NO;

问题五:如何解决不同尺寸视频容器大小变化?

如果后台接口返回真实的视频size就很好了,如果没有正确的size,那么就得手动算。

//获取视频尺寸
  AVURLAsset *asset = [AVURLAsset assetWithURL:[NSURL URLWithString:item.videoUrl]];
            NSArray *array = asset.tracks;
           
            
  for (AVAssetTrack *track in array) {
       if ([track.mediaType isEqualToString:AVMediaTypeVideo]) {
                    videoSize = track.naturalSize;
          }
  }

问题六:如何解决上滑下滑流畅展示?

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    dispatch_async(dispatch_get_main_queue(), ^{
        CGPoint translatedPoint = [scrollView.panGestureRecognizer translationInView:scrollView];
        //UITableView禁止响应其他滑动手势
        scrollView.panGestureRecognizer.enabled = NO;
        
       
        if(translatedPoint.y < -50 && self.index < (self.videoList.count - 1)) {
            self.index ++;   //向下滑动索引递增
        }
        
       
        
        if(translatedPoint.y > 50 && self.index > 0) {
            self.index --;   //向上滑动索引递减
        }
        
        
        
        
        
        [UIView animateWithDuration:0.15
                              delay:0.0
                            options:UIViewAnimationOptionCurveEaseOut animations:^{
                                //UITableView滑动到指定cell
                                [self.mTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.index inSection:self.section] atScrollPosition:UITableViewScrollPositionTop animated:NO];
                            } completion:^(BOOL finished) {
                                //UITableView可以响应其他滑动手势
                                scrollView.panGestureRecognizer.enabled = YES;
                            }];
        
    });
}

问题七:如何自定义播放视频控制视图交互?

布局的关键是   make.centerY.mas_equalTo(0);表示控件水平居中布局。

 [self.curTimeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.soundBtn.mas_right).mas_offset((3));
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(41);
    }];
[self.progressSlider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.curTimeLab.mas_right).mas_equalTo(8);
        make.right.mas_equalTo(self.totalTimeLab.mas_left).mas_equalTo(-8);
        make.top.bottom.mas_equalTo(0);
    }];

你可能感兴趣的:(个人OC知识梳理,ios)