iOS视频横竖屏正确打开形式:调整动画执行出错问题


问题回放


如果我们在一个iOS项目中使用到了视频播放器就难免会遇到强制横竖屏的问题.这个老生常谈的问题我们只需要监听设备的方向改变的通知即可,一般情况是不会出现什么的问题的.但是事实真的是这样吗?网上在这方面的资料也是杂乱不堪,大部分只是说说如何进行强制横竖屏,没有进一步说明动画操作,虽然大部分的情况是没有任何问题的,可是当用户快速的从左横屏→竖屏→右横屏的时候就会出现PlayView的尺寸显示不正确的问题了.原因是当左横屏→竖屏的动画还未完成时(动画过程需要0.3s),设备已经完成了从竖屏到右竖屏的过程了.导致两个动画同时执行,界面就出现问题.当然了,还有如下的问题也是类似的.都是尺寸缩放动画错乱造成的!

错误代码如下
#define KmainWidth  [UIScreen mainScreen].bounds.size.width
#define KmainHeight  [UIScreen mainScreen].bounds.size.height
//添加监听
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(rotateScreenViews:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];
#pragma mark - 屏幕旋转的主要动画操作方法

- (void)rotateScreenViews:(NSObject *)sender{

    UIDevice* device = [sender valueForKey:@"object"];
    if (device.orientation == UIDeviceOrientationPortrait ) {
        self.playView.frame = CGRectMake(0, 0, KmainWidth, KmainWidth/16.0*9);
    }
    if (device.orientation == UIDeviceOrientationLandscapeLeft ) {
        self.playView.frame = CGRectMake(0, 0, KmainWidth, KmainHeight);
    }
    if (device.orientation == UIDeviceOrientationLandscapeRight ) {
        self.playView.frame = CGRectMake(0, 0, KmainWidth, KmainHeight);
    }
}
* 正常情况(此时宽高比:16:9)
* 动画执行错乱情况(此时宽高比:横屏情况下的宽高比)


解决思路


上面说到的这种问题我们该怎么解决呢?其实很简单,我们来自定义动画.通过布尔值(isPortraitFinished)来监控屏幕旋转的动画是否完成,如果完成我们才执行接下来的动画操作,或者结束操作.而且这里我加入了另外的一个布尔值(isFullScreen)来减少执行动画的次数.具体代码如下所示.

在.m的类扩展中有以下属性.

#define KmainWidth  [UIScreen mainScreen].bounds.size.width
#define KmainHeight  [UIScreen mainScreen].bounds.size.height

@interface FullScreenViewController ()

@property(nonatomic,strong)UILabel *playView;
@property(nonatomic,assign)BOOL isFullScreen;//是否是全屏,减少动画执行次数
@property(nonatomic,assign)BOOL isPortraitFinished;//是否已经完成了竖屏.

@end

viewDidLoad主要是来进行视频View的初始化,注册旋转通知等,如下所示.

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.playView];
    self.isPortraitFinished = YES;//这个要开始设置为YES,不管在横屏还是竖屏的情况下都可以适应.
    //注册横竖屏的通知
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(rotateScreenViews:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];
}

- (UILabel *)playView{
    
    if (_playView == nil) {
        _playView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, KmainWidth, KmainWidth/16.0*9)];
        _playView.backgroundColor = [UIColor lightGrayColor];
        _playView.text = @"播放内容";
        _playView.font = [UIFont systemFontOfSize:18];
        _playView.textAlignment = NSTextAlignmentCenter;
    }
    return _playView;
}

viewWillAppear中先设定屏幕的设备方向为竖屏方向,如下所示.

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    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 = UIInterfaceOrientationPortrait;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}

现在正题来了,我们的旋转方法相比于问题回放中代码有何改变和优化呢? 这里首先进行第一步通知回调方法的实现,代码如下所示.

- (void)rotateScreenViews:(NSObject *)sender{

    UIDevice* device = [sender valueForKey:@"object"];
    
    if (device.orientation == UIDeviceOrientationPortrait && _isFullScreen) {
        CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;//时间
        _isFullScreen = NO;
        [UIView animateWithDuration:duration animations:^{
            CGFloat nowWidth = 0;
            if (KmainWidth > KmainHeight) {
                nowWidth = KmainHeight;
            }else{
                nowWidth = KmainWidth;
            }
            NSLog(@"竖进行");
            self.playView.frame = CGRectMake(0, 0, nowWidth, nowWidth/16.0*9);
            self.isPortraitFinished = NO;
        } completion:^(BOOL finished) {
            NSLog(@"竖完成");
            self.isPortraitFinished = YES;
            [self portraitFinishaNextAction];
            
        }];
        
    }
    
    if (device.orientation == UIDeviceOrientationLandscapeLeft && !_isFullScreen) {
        CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;//时间
        _isFullScreen = YES;
        [UIView animateWithDuration:duration animations:^{
            if (self.isPortraitFinished != NO) {
                self.playView.frame = CGRectMake(0, 0, KmainWidth, KmainHeight);
                NSLog(@"左右进行");
            }
        }completion:^(BOOL finished) {
            NSLog(@"左右完成");
        }];
    }
    if (device.orientation == UIDeviceOrientationLandscapeRight && !_isFullScreen) {
        CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;//时间
        _isFullScreen = YES;
        [UIView animateWithDuration:duration animations:^{
            if (self.isPortraitFinished != NO) {
                self.playView.frame = CGRectMake(0, 0, KmainWidth, KmainHeight);
                NSLog(@"左右进行");
            }
        } completion:^(BOOL finished) {
        }];
    }
}

在上一方法中,我们通过 isPortraitFinished 属性来隔断动画的执行.然后用户快速的旋转屏幕的话,我们会在第二个方法( portraitFinishaNextAction )中处理这种情况,当竖屏动画完成之后,我们还要判断当前设备的方向是否是竖屏,如果是,那么没有动画执行,如果不是的话,那么我们就再次执行旋转动画.代码如下所示.

//竖屏完成之后判断现在的手机设备的方向,如果是横竖屏,那么需要再次进行动画操作.
-(void)portraitFinishaNextAction{
    UIDeviceOrientation  orient = [UIDevice currentDevice].orientation;
    
    if (orient == UIDeviceOrientationLandscapeLeft || orient == UIDeviceOrientationLandscapeRight) {
        CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;//时间
        
        [UIView animateWithDuration:duration delay:duration options:UIViewAnimationOptionLayoutSubviews animations:^{
            
            if (self.isPortraitFinished != NO) {
                self.playView.frame = self.playView.frame;
            }
        } completion:^(BOOL finished) {
            self.playView.frame = CGRectMake(0, 0, KmainWidth, KmainHeight);
        }];
    }
}

好了,这里就完成我们的屏幕旋转动画的优化处理了,Demo链接地址会在文章尾部给出.


屏幕旋转的补充说明


这里有几点需要补充的,假定当期的播放页面可以在横屏的情况下进行返回上一个页面的操作.这时候我们需要先旋转屏幕,我们该如何实现呢?我们只需要在viewWillDisappear中写入如下代码即可.

//假设有上一级的页面 我们通过下面代码来进行竖屏切换,如果没有 去掉就好
- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];

    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 = UIInterfaceOrientationPortrait;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}

第二补充点就是我们如果做锁屏操作怎么做才合适呢?比如我现在的项目中就同时有直播和点播,难道我要写两套代码吗?不,我们只需要在AppDelegate中设定设备是否可以旋转,以及设备旋转的方向,这里的值我们可以随意设定代码如下所示.

AppDelegate.h中的属性代码

@property (nonatomic,assign)NSInteger allowRotate;

AppDelegate.m中的实现代码

#pragma mark - 设备是否可以旋转
//此方法会在设备横竖屏变化的时候调用
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{

    if (_allowRotate == 111) {
        return UIInterfaceOrientationMaskLandscape;
    }
    
    if (_allowRotate == 999) {
        return UIInterfaceOrientationMaskLandscapeRight;
    }
    
    if (_allowRotate == 666) {
        return UIInterfaceOrientationMaskLandscapeLeft;
    }
    
    if (_allowRotate == 333) {
        return UIInterfaceOrientationMaskPortrait;
    }
    
    if (_allowRotate == 1) {
        return UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskPortrait;
    }else{
        return (UIInterfaceOrientationMaskPortrait);
    }
}


// 返回是否支持设备自动旋转
- (BOOL)shouldAutorotate
{
    if (_allowRotate == 999 || _allowRotate == 666) {
        return NO;
    }
    
    if (_allowRotate == 1) {
        return YES;
    }
    return NO;
}

这样我们只需要操作AppDelegate中的allowRotate属性即可.


结语


好了,这一次分享就到这里了.如果喜欢的话,欢迎点赞收藏,当然了,如果有任何问题都可以联系骚栋.或者在评论区提出来,我会及时回复的,谢谢.最后还是老惯例,附上实现代码的传送门.

FullScreenDemo传送门🚪
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,306评论 4 370
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,657评论 2 307
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,928评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,688评论 0 220
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,105评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,024评论 1 225
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,159评论 2 318
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,937评论 0 212
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,689评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,851评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,325评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,651评论 3 263
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,364评论 3 244
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,192评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,985评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,154评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,955评论 2 279

推荐阅读更多精彩内容