iPhone旋转

本文介绍了如何在iOS应用中处理视频播放时的设备旋转问题,包括通过监听状态栏旋转变化来调整视频视图大小,以及如何在不同旋转情况下实现全屏和竖屏播放的切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Type One

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:)name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

- (void)statusBarOrientationChange:(id)sender{
    self.navigationController.navigationBar.hidden = NO;
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    if (orientation == UIDeviceOrientationPortraitUpsideDown ) {
        self.playerView.frame = CGRectMake(0,0, ScreenWidth, AREA_HEIGHT(422));
        [self.view updateConstraints];
    };
}

Type Two

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (nonatomic, assign) BOOL allowRotation;//是否允许转向

@end



- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window{


    //只有在视频的播放页面才允许旋转。所以。当允许旋转的时候。
    //如果旋转了就全屏播放视频。 不允许旋转就是竖屏的。
    if (_allowRotation == YES) {

        UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
        if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
            return UIInterfaceOrientationMaskLandscape;
        }else { // 横屏后旋转屏幕变为竖屏
            return UIInterfaceOrientationMaskPortrait;
        }

    }else{
        return (UIInterfaceOrientationMaskPortrait);
    }
}

Type Three


@implementation UINavigationController (VideoPlay)

//步骤四:如果Viewcontroller在Navigationcontroller中,需要创建NavigationgController的分类重写以下方法
-(BOOL)shouldAutorotate {
    return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations {
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值