IOS指定页面旋转

博主在做视频全屏播放项目时,因应用整体不支持横屏,需关闭其他页面屏幕自适应旋转,开启视频页面旋转。文中给出实现思路,要在AppDelegate.m中实现方法,在特定方法里注册监听,在需旋转页面发通知,退出页面发关闭旋转通知。

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

本人在做视频全屏播放的项目中遇到的问题,应用整体是不支持横屏的。因为全屏使用的是触发手机横屏的机制,所以其他页面需要关闭屏幕自适应旋转,视频页面则开启屏幕的自适应旋转。实现代码如下:

在AppDelegate.m 中实现下面方法

- (void)initializeTheNotificationCenter
{
    //在进入需要全屏的界面里面发送允许横屏的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startFullScreen) name:kStartFullScreen object:nil];//允许横屏
    
    //在退出允许横屏的界面里面发送退出横屏的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen) name:kendFullScreen object:nil];//退出横屏
}

#pragma mark 允许横屏
-(void)startFullScreen
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotation = YES;
}

#pragma mark    退出横屏
-(void)endFullScreen
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotation = NO;
    
    //强制归正:
    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];
    }
}

#pragma mark    禁止横屏
- (UIInterfaceOrientationMask )application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (self.allowRotation) {
        return UIInterfaceOrientationMaskAll;
    }
    return UIInterfaceOrientationMaskPortrait;
}

记得在

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 中注册监听
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
....
[self initializeTheNotificationCenter];
...

然后在需要旋转的页面发送通知,退出页面发送关闭旋转通知

    [[NSNotificationCenter defaultCenter] postNotificationName:kStartFullScreen object:nil];

    [[NSNotificationCenter defaultCenter] postNotificationName:kendFullScreen object:nil];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值