AppDelegate
AppDelegate.h
添加属性allowAcrolls
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow * window;
@property (nonatomic,assign)BOOL allowAcrolls;//控制横竖屏显示
@endm
AppDelegate.m
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
{
if (self.allowAcrolls == NO) {//若游戏为横屏,此处不变,若游戏为竖屏,此处将NO改为YES
//横屏
return UIInterfaceOrientationMaskLandscape;
}else{
//竖屏
return UIInterfaceOrientationMaskPortrait;
}
}
使用场景
添加观察者
// 横竖屏切换通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleScreenOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
#pragma mark ––––––––––––––––– 横竖屏切换通知
/// 横竖屏切换通知
- (void)handleScreenOrientationChange:(NSNotification *)noti {
if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationPortraitUpsideDown) {
// 更新竖屏UI
[self updateVerticalSubviews];
} else {
// 更新横屏UI
[self updateHorizontalSubviews];
}
}
移除观察者
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
改变方向
//设置真机方向
- (void)setDeviceOrientation:(BOOL)isHorizontal{
/**
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
UIInterfaceOrientationMaskPortrait // 设备(屏幕)直立
UIInterfaceOrientationMaskLandscapeLeft 屏幕向左横置
UIInterfaceOrientationMaskLandscapeRight 屏幕向右橫置
UIInterfaceOrientationMaskPortraitUpsideDown 未知
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
}
*/
if(isHorizontal){//横屏
// AppDelegate设置横屏
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
// AppDelegate中锁定为横屏
app.allowAcrolls = NO;
// 强制旋转
if (@available(iOS 16.0, *)) {
#if defined(__IPHONE_16_0)
// 避免没有更新Xcode14的同事报错
// iOS16新API,让控制器刷新方向,新方向为上面设置的orientations
[[[XXYCustomUtil sharedInstance] getCurrentVC] setNeedsUpdateOfSupportedInterfaceOrientations];
#endif
} else {
// iOS16以下
// NSNumber *orientationPortrait = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
// [[UIDevice currentDevice] setValue:orientationPortrait forKey:@"orientation"];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:@selector(setOrientation:)]];
invocation.selector = NSSelectorFromString(@"setOrientation:");
invocation.target = [UIDevice currentDevice];
int initOrientation = UIDeviceOrientationLandscapeLeft;
[invocation setArgument:&initOrientation atIndex:2];
[invocation invoke];
}
}else{//竖屏
// AppDelegate设置竖屏
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
// 横屏屏-强制屏幕竖屏
// AppDelegate中锁定为竖屏
app.allowAcrolls = YES;
// 强制旋转
if (@available(iOS 16.0, *)) {
#if defined(__IPHONE_16_0)
// 避免没有更新Xcode14的同事报错
// iOS16新API,让控制器刷新方向,新方向为上面设置的orientations
[[[XXYCustomUtil sharedInstance] getCurrentVC]setNeedsUpdateOfSupportedInterfaceOrientations];
#endif
} else {
// iOS16以下
// NSNumber *orientationPortrait = [NSNumber numberWithInt:UIDeviceOrientationPortrait];
// [[UIDevice currentDevice] setValue:orientationPortrait forKey:@"orientation"];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:@selector(setOrientation:)]];
invocation.selector = NSSelectorFromString(@"setOrientation:");
invocation.target = [UIDevice currentDevice];
int initOrientation = UIDeviceOrientationPortrait;
[invocation setArgument:&initOrientation atIndex:2];
[invocation invoke];
}
}
}
网友评论