当前位置:网站首页>Player screen orientation scheme

Player screen orientation scheme

2022-06-12 13:40:00 Li. CQ

Similar to the current mainstream app The vertical and horizontal switching mode

  1. principle : When the player is horizontal , present A new view controller , The new controller supports horizontal and vertical screens . dismiss When , Return to the previous player ;
  2. Source code :

// - Statement

#import <UIKit/UIKit.h>
#import "DYVideoPlayerView+Private.h"

#pragma mark -  Horizontal screen controller 
@interface DYVideoPlayerVC : UIViewController

/**  Initialization method  */
- (instancetype)initWithVideoPlayerView:(UIView *)videoPlayerView preferredInterfaceOrientationForPresentation:(UIInterfaceOrientation)orientation;

@end

#pragma mark -  Jump to the transition animation of the horizontal screen controller 
typedef NS_ENUM(NSUInteger, DYVideoPlayerTransitionType){
    DYVideoPlayerTransitionTypePresent = 0,
    DYVideoPlayerTransitionTypeDismiss,
};

@interface DYVideoPlayerTransition : NSObject<UIViewControllerAnimatedTransitioning>
+(instancetype)presentTransitionWithVideoPlayerView:(DYVideoPlayerView *)videoPlayerView;
+(instancetype)dismissTransitionWithVideoPlayerView:(DYVideoPlayerView *)videoPlayerView;
@end

// - DYVideoPlayerView+Private.h

#import "DYVideoPlayerView.h"

@interface DYVideoPlayerView ()

/**  Parent view before full screen  */
@property (nonatomic, weak) UIView *originalSuperView;

/**  Before full screen  frame */
@property (nonatomic, assign) CGRect originalFrameInSuperView;

/**  Before full screen  frame */
@property (nonatomic, assign) CGRect originalFrameInWindow;

@end

// - Realization

#import "DYVideoPlayerVC.h"

#pragma mark -  Horizontal screen controller 
@interface DYVideoPlayerVC ()
/**  player  */
@property (nonatomic, assign) UIView *videoPlayerView;

/**  Jump to the parent view of the previous player  */
@property (nonatomic, assign) UIView *videoPlayerViewOriginalSuperView;

/**  The direction of the jump controller  */
@property (nonatomic, assign) UIInterfaceOrientation orientation;

@end

@implementation DYVideoPlayerVC
/**  Initialization method  */
- (instancetype)initWithVideoPlayerView:(UIView *)videoPlayerView preferredInterfaceOrientationForPresentation:(UIInterfaceOrientation)orientation
{
    self = [super init];
    if (self) {
        self.modalPresentationStyle = UIModalPresentationFullScreen;
        self.videoPlayerView = videoPlayerView;
        self.transitioningDelegate = self.videoPlayerView;
        self.videoPlayerViewOriginalSuperView = self.videoPlayerView .superview;
        self.orientation = orientation;
    }
    return self;
}

// - MARK: <--  Method of initializing settings  -->
- (void)viewDidLoad {
    [super viewDidLoad];
}

-(void)viewWillAppear:(BOOL)animated{
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
}

- (BOOL)shouldAutorotate{
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return self.orientation;
}

@end

#pragma mark -  Jump to the transition animation of the horizontal screen controller 
@interface DYVideoPlayerTransition ()

/**  Jump type  */
@property (nonatomic, assign)DYVideoPlayerTransitionType transitionType;

/** present When   originalRect-> Full screen , dismiss When  :  Full screen ->originalRect*/
@property (nonatomic, weak) DYVideoPlayerView *videoPlayerView;

@end

@implementation DYVideoPlayerTransition

// - MARK: <--  Generate present and dismiss The transition animation of  -->
+(instancetype)presentTransitionWithVideoPlayerView:(DYVideoPlayerView *)videoPlayerView{
    videoPlayerView.originalSuperView = videoPlayerView.superview;
    videoPlayerView.originalFrameInSuperView = videoPlayerView.frame;
    videoPlayerView.originalFrameInWindow = [videoPlayerView.originalSuperView convertRect:videoPlayerView.originalFrameInSuperView toView:[UIApplication sharedApplication].keyWindow];
    DYVideoPlayerTransition *transition = [[DYVideoPlayerTransition alloc] init];
    transition.videoPlayerView = videoPlayerView;
    transition.transitionType = DYVideoPlayerTransitionTypePresent;
    return transition;
}

+(instancetype)dismissTransitionWithVideoPlayerView:(DYVideoPlayerView *)videoPlayerView{
    DYVideoPlayerTransition *transition = [[DYVideoPlayerTransition alloc] init];
    transition.videoPlayerView = videoPlayerView;
    transition.transitionType = DYVideoPlayerTransitionTypeDismiss;
    return transition;
}

// - MARK: <--  Transition animation proxy method  -->
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return 3;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    if (_transitionType == DYVideoPlayerTransitionTypePresent) {
        [self presentAnimation:transitionContext];
    }else{
        [self dismissAnimation:transitionContext];
    }
}

/** present  Animation */
- (void)presentAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    // -  The jump vc
    DYVideoPlayerVC *toVC = (DYVideoPlayerVC *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    // -  The jump vc Of view
    UIView *containerView = [transitionContext containerView];
    UIView *toView = toVC.view;
    [containerView addSubview:toView];

    // -  Set the original position 
    CGRect originalRect;
    CGRect tmpRect = self.videoPlayerView.originalFrameInWindow;
    if (toVC.orientation == UIInterfaceOrientationLandscapeLeft) {
        toView.transform = CGAffineTransformMakeRotation(M_PI / 2);
        originalRect = CGRectMake(
                                  [UIApplication sharedApplication].keyWindow.frame.size.width -  - tmpRect.size.height,
                                  tmpRect.origin.x,
                                  tmpRect.size.height,
                                  tmpRect.size.width);
    }else{
        toView.transform = CGAffineTransformMakeRotation(-M_PI / 2);
        originalRect = CGRectMake(tmpRect.origin.y, tmpRect.origin.x, tmpRect.size.height, tmpRect.size.width);

    }
    
    // -  Change the parent view and set the position 
    [toView addSubview:self.videoPlayerView];
    toView.frame = originalRect;
    self.videoPlayerView.frame = toView.bounds;
    [self.videoPlayerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(toView);
    }];

    // -  Jump animation 
    NSTimeInterval duration = [self transitionDuration:transitionContext];
    [UIView animateWithDuration:duration animations:^{
        toView.transform = CGAffineTransformIdentity;
        toView.frame = [UIApplication sharedApplication].keyWindow.bounds;
        [toView layoutIfNeeded];
    } completion:^(BOOL finished) {
        toView.transform = CGAffineTransformIdentity;
        toView.frame = [UIApplication sharedApplication].keyWindow.bounds;
        [transitionContext completeTransition:YES];
    }];
}

/** dismiss Animation  */
- (void)dismissAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    // -  Current vc
    DYVideoPlayerVC *fromVC = (DYVideoPlayerVC *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    // -  Current view
    UIView *containerView = [transitionContext containerView];
    UIView *fromView = fromVC.view;
    UIView *toView = toVC.view;
    [containerView addSubview:fromView];
    
    toView.frame = [UIApplication sharedApplication].keyWindow.bounds;
    [containerView insertSubview:toView belowSubview:fromView];
    
    // -  Animation returned 
    NSTimeInterval duration = [self transitionDuration:transitionContext];
    [UIView animateWithDuration:duration animations:^{
        fromView.transform = CGAffineTransformIdentity;
        fromView.frame = self.videoPlayerView.originalFrameInWindow;
        [fromView layoutIfNeeded];
    } completion:^(BOOL finished) {
        fromView.transform = CGAffineTransformIdentity;
        fromView.frame = self.videoPlayerView.originalFrameInWindow;
        
        // -  Change the parent view and set the position 
        [fromVC.videoPlayerViewOriginalSuperView addSubview:fromVC.videoPlayerView];
        fromView.frame = self.videoPlayerView.originalFrameInSuperView;
        [fromVC.videoPlayerView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(fromVC.videoPlayerViewOriginalSuperView).offset(self.videoPlayerView.originalFrameInSuperView.origin.x);
            make.top.mas_equalTo(fromVC.videoPlayerViewOriginalSuperView).offset(self.videoPlayerView.originalFrameInSuperView.origin.y);
            make.width.mas_equalTo(self.videoPlayerView.originalFrameInSuperView.size.width);
            make.height.mas_equalTo(self.videoPlayerView.originalFrameInSuperView.size.height);
        }];
        [transitionContext completeTransition:YES];
    }];
}


@end

// - Use

//
//  DYVideoPlayerView.m
//  QEZB
//
//  Created by  Li Chaoqun  on 2018/4/23.
//Copyright  2018 year  zhou. All rights reserved.
//

#pragma mark ************************************  Short video player  view ************************************

#import "DYVideoPlayerView.h"
#import "DYVideoPlayerVC.h"

@interface DYVideoPlayerView () <UIViewControllerTransitioningDelegate>

/** <# notes #> */
@property (nonatomic, weak) UIViewController *videoPlayerVC;

@end

@implementation DYVideoPlayerView

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self toRightScreenModel];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil];

    }
    return self;
}

- (void)statusBarOrientationChange:(NSNotification *)notification {
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    if (orientation == UIDeviceOrientationLandscapeLeft ) {
        [self toLeftScreenModel];
    }else if (orientation == UIDeviceOrientationLandscapeRight) {
        [self toRightScreenModel];
    }else if (orientation == UIDeviceOrientationPortrait) {
        [self toSmallScreenModel];
    }
}

-(void)toSmallScreenModel{
    self.videoPlayerVC.transitioningDelegate = self;
    [self.videoPlayerVC dismissViewControllerAnimated:YES completion:nil];
    self.videoPlayerVC = nil;
}


-(void)toLeftScreenModel{
    // -  If you are already in full screen mode ,  Do not operate 
    if (self.videoPlayerVC) return;

    // -  Rotate to large screen callback 
    if ([self.delegate respondsToSelector:@selector(playerView:willRotateToFullScreen:)]) {
        [self.delegate playerView:self willRotateToFullScreen:YES];
    }

    // -  Jump to the big screen 
    DYVideoPlayerVC *videoPlayerVC = [[DYVideoPlayerVC alloc]initWithVideoPlayerView:self preferredInterfaceOrientationForPresentation:UIInterfaceOrientationLandscapeRight];
    UIImageView *tempV = [[UIImageView alloc]initWithImage:[UIImage imageCaptureTheFullScreen]];
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:tempV];
    [[ControllerTool getCurrentVC] presentViewController:videoPlayerVC animated:YES completion:^{
        [tempV removeFromSuperview];
    }];
    self.videoPlayerVC = videoPlayerVC;
}
/**  Rotate to large screen mode ( On the right ) */
-(void)toRightScreenModel{
    // -  If you are already in full screen mode ,  Do not operate 
    if (self.videoPlayerVC) return;

    // -  Rotate to large screen callback 
    if ([self.delegate respondsToSelector:@selector(playerView:willRotateToFullScreen:)]) {
        [self.delegate playerView:self willRotateToFullScreen:YES];
    }

    // -  Jump to the big screen 
    DYVideoPlayerVC *videoPlayerVC = [[DYVideoPlayerVC alloc]initWithVideoPlayerView:self preferredInterfaceOrientationForPresentation:UIInterfaceOrientationLandscapeLeft];
    UIImageView *tempV = [[UIImageView alloc]initWithImage:[UIImage imageCaptureTheFullScreen]];
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:tempV];
    [[ControllerTool getCurrentVC] presentViewController:videoPlayerVC animated:YES completion:^{
        [tempV removeFromSuperview];
    }];
    self.videoPlayerVC = videoPlayerVC;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    return [DYVideoPlayerTransition presentTransitionWithVideoPlayerView:self];
}

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    return [DYVideoPlayerTransition dismissTransitionWithVideoPlayerView:self];
}

@end

原网站

版权声明
本文为[Li. CQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121325425838.html