Video控件横屏显示

视频全屏demo

点击视频容器,让其全屏横屏显示到window上。

视频容器View

#import 

typedef NS_ENUM(NSUInteger, VideoState) {
    VideoViewStateSmall,
    VideoViewStateAnimating,
    VideoViewStateFullscreen,
};

@interface VideoView : UIView
/**
 记录VideoView的原父视图
 */
@property (nonatomic, weak) UIView *videoParentView;

/**
 记录VideoView的原frame
 */
@property (nonatomic, assign) CGRect videoViewFrame;

/**
 记录VideoView的状态
 */
@property (nonatomic, assign) VideoState state;
@end
#import "VideoView.h"

@implementation VideoView

- (instancetype)init
{
    if (self = [super init]) {
        self.backgroundColor = [UIColor yellowColor];
        //初始化视频控件
    }
    return self;
}
@end

将视频容器放到vc中

#import 

@interface VideoViewController : UIViewController

@end

#import "VideoViewController.h"
#import "VideoView.h"
@interface VideoViewController ()
@property (nonatomic, strong) VideoView *videoView;
@end

@implementation VideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.videoView = [[VideoView alloc]init];
    self.videoView.frame = CGRectMake(0, 100, self.view.frame.size.width, 180);
    [self.view addSubview:self.videoView];
    
    UITapGestureRecognizer *tapGestureRecongnizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    [self.videoView addGestureRecognizer:tapGestureRecongnizer];
}

- (void)handleTapGesture:(UITapGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateEnded) {
        if (self.videoView.state == VideoViewStateSmall) {
            [self enterFullScreen];
        }
        else if (self.videoView.state == VideoViewStateFullscreen){
            [self exitFullScreen];
        }
    }
}

- (void)enterFullScreen
{
    if (self.videoView.state != VideoViewStateSmall) {
        return;
    }
    
    self.videoView.state = VideoViewStateAnimating;
    /*
     * 记录进入全屏前的parentView和frame
     */
    self.videoView.videoParentView = self.videoView.superview;
    self.videoView.videoViewFrame = self.videoView.frame;
    
    /*
     * movieView移到window上
     */
    CGRect rectInWindow = [self.videoView convertRect:self.videoView.bounds toView:[UIApplication sharedApplication].keyWindow];
    [self.videoView removeFromSuperview];
    self.videoView.frame = rectInWindow;
    [[UIApplication sharedApplication].keyWindow addSubview:self.videoView];
    
    /*
     * 执行动画
     */
    [UIView animateWithDuration:0.35 animations:^{
        self.videoView.transform = CGAffineTransformMakeRotation(M_PI_2);
        self.videoView.bounds = CGRectMake(0, 0, CGRectGetHeight(self.videoView.superview.bounds), CGRectGetWidth(self.videoView.superview.bounds));
        self.videoView.center = CGPointMake(CGRectGetMidX(self.videoView.superview.bounds), CGRectGetMidY(self.videoView.superview.bounds));
    } completion:^(BOOL finished) {
        self.videoView.state = VideoViewStateFullscreen;
    }];
    
    [self refreshStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
}

- (void)exitFullScreen
{
    if (self.videoView.state != VideoViewStateFullscreen) {
        return;
    }
    
    self.videoView.state = VideoViewStateAnimating;
    
    CGRect frame = [self.videoView.videoParentView convertRect:self.videoView.videoViewFrame toView:[UIApplication sharedApplication].keyWindow];

    [UIView animateWithDuration:0.35 animations:^{
        self.videoView.transform = CGAffineTransformIdentity;
        self.videoView.frame = frame;
    } completion:^(BOOL finished) {
        /*
         * movieView回到小屏位置
         */
        [self.videoView removeFromSuperview];
        self.videoView.frame = self.videoView.videoViewFrame;
        [self.videoView.videoParentView addSubview:self.videoView];
        self.videoView.state = VideoViewStateSmall;
    }];
    
    [self refreshStatusBarOrientation:UIInterfaceOrientationPortrait];
}

- (void)refreshStatusBarOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    [[UIApplication sharedApplication] setStatusBarOrientation:interfaceOrientation animated:YES];
}

- (BOOL)shouldAutorotate {
    return NO;
}

@end

你可能感兴趣的:(Video控件横屏显示)