iOS横屏实践 2022-04-15 周五

2022-04-15  本文已影响0人  勇往直前888

简介

工程配置

工程配置 Info.plist

代码配置

*AppDelegate只有一个,并且系统提供了类似单例的访问方法,这为修改带来了方便。

@interface AppDelegate : UIResponder <UIApplicationDelegate>

// 与SceneDelegate中的window是同一个;
// 很多第三方库,比如SVProgressHUD要用到;
// 自己以view的方式做弹窗也要用到;
@property (nonatomic, strong) UIWindow *window;

// NO:只支持竖屏;YES:支持横屏(Home键在右)
@property (nonatomic, assign) BOOL isLandscapeRight;

@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    // 默认不支持横屏,只支持竖屏
    self.isLandscapeRight = NO;
    
    return YES;
}

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    // 大多数情况只支持竖屏,少部分情况只支持横屏(Home键向右)
    if (self.isLandscapeRight) {
        return UIInterfaceOrientationMaskLandscapeRight;
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

旋转手机

这真的是很扯淡的事情。
iOS强制改变物理设备方向的进阶方法

// 旋转手机
+ (void)forceToOrientation:(UIDeviceOrientation)orientation {
    NSNumber *orientationTarget = [NSNumber numberWithInteger:orientation];
    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}

比如,你在AppDelegate.m设置只能竖屏的Mask,但是你却要求横屏向右旋转,结果当然是没效果。(自相矛盾啊)

// 旋转到横屏
+ (void)rotateToLandscapeRight {
    // 先设置Mask
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.isLandscapeRight = YES;
    
    // 然后旋转手机;方向需要在Mask的范围之内
    [PDADevice forceToOrientation:UIDeviceOrientationLandscapeRight];
}

// 旋转到竖屏
+ (void)rotateToPortrait {
    // 先设置Mask
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.isLandscapeRight = NO;
    
    // 然后旋转手机;方向需要在Mask的范围之内
    [PDADevice forceToOrientation:UIDeviceOrientationPortrait];
}

调用时机

由于绝大多数页面只需要竖屏就可以了,这个只要在工程配置的地方配置一下就行。
对于需要横屏的页面,可以在viewWillAppear中设置为横屏,然后在viewWillDisappear中设置为竖屏就可以了。

- (void)viewWillAppear:(BOOL)animated {
    /*
     * 设置横屏
     */
    [PDADevice rotateToLandscapeRight];
}

- (void)viewWillDisappear:(BOOL)animated {
    /*
     * 设置竖屏
     */
    [PDADevice rotateToPortrait];
}

参考文章

iOS固定APP 某个界面强制横屏或者竖屏

iOS强制改变物理设备方向的进阶方法

iOS 不勾选设置工程Landscape,实现某个界面强制横屏

iOS设置某个界面强制横屏,进入就横屏

失效的屏幕旋转方法

- (BOOL)shouldAutorotate {
    return NO; // 注意:如果是只支持一种,比如只支持横屏,这里需要设置为NO;YES的至少需要两种或者以上的Mask才行。
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeRight;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeRight;
}
// New Autorotation support.
@property(nonatomic, readonly) BOOL shouldAutorotate API_AVAILABLE(ios(6.0)) API_UNAVAILABLE(tvos);
@property(nonatomic, readonly) UIInterfaceOrientationMask supportedInterfaceOrientations API_AVAILABLE(ios(6.0)) API_UNAVAILABLE(tvos);
// Returns interface orientation masks.
@property(nonatomic, readonly) UIInterfaceOrientation preferredInterfaceOrientationForPresentation API_AVAILABLE(ios(6.0)) API_UNAVAILABLE(tvos);

吐槽

上一篇 下一篇

猜你喜欢

热点阅读