当前位置:网站首页>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
- 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 ;
- 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
边栏推荐
- Software construction 03 regular expression
- How to balance multiple losses in deep learning?
- A brief introduction to Verilog mode
- D1 哪吒开发板 了解基本的启动加载流程
- 智能垃圾桶语音芯片应用设计方案介绍,WT588F02B-8S
- "Non" reliability of TCP
- 一种快速创建测试窗口的方法
- Experience and learning path of introductory deep learning and machine learning
- 618进入后半段,苹果占据高端市场,国产手机终于杀价竞争
- C language implementation of string and memory library functions
猜你喜欢

Scyther工具形式化分析Woo-Lam协议
FFmpeg 学习指南

Successfully rated Tencent t3-2, 10000 word parsing

D1 Nezha Development Board understands the basic startup and loading process
![[brush title] probability of winning a draw](/img/f5/7e8dac944876f920db10508ec38e92.png)
[brush title] probability of winning a draw

Web3.0, the era of "stimulating creativity"

Overview of embedded system 2- composition and application of embedded system
![[wechat applet development] Part 1: development tool installation and program configuration](/img/a8/f4dcbde295ba7cf738d878464b3af0.png)
[wechat applet development] Part 1: development tool installation and program configuration

It is enough to read this article. Web Chinese development

Stm32f1 and stm32cubeide programming examples - device driver -eeprom-at24c256 driver
随机推荐
"Non" reliability of TCP
[wechat applet development] Part 1: development tool installation and program configuration
1001:Hello,World
There was an error installing mysql. Follow the link below to CMD
C#DBHelper_ FactoryDB_ GetConn
Codeforces 1637 A. sorting parts - simple thinking
数据类型转换和条件控制语句
Codeforces 1629 B. GCD arrays - simple thinking
It is enough to read this article. Web Chinese development
Innovation training (x) advanced interface beautification
Behind the unsealing of Shanghai, this group of developers "cloud gathering" built an AI anti epidemic robot
Software construction 03 regular expression
Seeking magic square of order n with C language
Pytoch official fast r-cnn source code analysis (I) -- feature extraction
Stm32f1 and stm32subeide programming example - device driver -dht11 temperature sensor driver
Application of bit operation in C language
Code debugging - print log output to file
Record some settings for visual studio 2019
Stm32f1 and stm32cubeide programming examples - device driver -eeprom-at24c256 driver
1414: [17noip popularization group] score