添加观察者
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
#pragma mark ––––––––––––––––– 监测横竖屏切换
#pragma mark ––––––––––––––––– 监测横竖屏切换
- (void)deviceOrientationDidChange
{
NSLog(@"deviceOrientationDidChange:%ld",(long)[UIDevice currentDevice].orientation);
//1.获取 当前设备 实例
UIDevice *device = [UIDevice currentDevice];
/**
device.orientation
UIDeviceOrientationUnknown, //系統無法判斷目前Device的方向,有可能是斜置
UIDeviceOrientationPortrait, // 屏幕直立,home键朝下
UIDeviceOrientationPortraitUpsideDown, // 屏幕直立,上下顛倒,home键在上
UIDeviceOrientationLandscapeLeft, // 屏幕向左横置,home键在右
UIDeviceOrientationLandscapeRight, // 屏幕向右横置,home键在左
UIDeviceOrientationFaceUp, // 屏幕朝上平躺
UIDeviceOrientationFaceDown // 屏幕朝下平躺
*/
// NSLog(@"deviceOrientationDidChange============ %ld",(long)[UIDevice currentDevice].orientation);
if(device.orientation == UIDeviceOrientationUnknown || device.orientation == UIDeviceOrientationPortrait){//竖屏
[self updateVerticalSubviews];//更新竖屏UI
}else if(device.orientation == UIDeviceOrientationFaceUp || device.orientation == UIDeviceOrientationFaceDown){//屏幕朝上-屏幕朝下
}else{//横屏
[self updateHorizontalSubviews];//更新横屏样式
}
}
移除观察者
[[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIDeviceOrientationDidChangeNotification
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){//横屏
if (@available(iOS 16.0, *)) {
[[[XXYCustomUtil sharedInstance] getCurrentVC] setNeedsUpdateOfSupportedInterfaceOrientations];
NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
UIWindowScene *ws = (UIWindowScene *)array[0];
UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] init];
geometryPreferences.interfaceOrientations = UIInterfaceOrientationMaskLandscapeRight;
[self updateHorizontalSubviews];
[ws requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:^(NSError * _Nonnull error) {
//业务代码
}];
} else {
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
}
}else{//竖屏
if (@available(iOS 16.0, *)) {
[[[XXYCustomUtil sharedInstance] getCurrentVC] setNeedsUpdateOfSupportedInterfaceOrientations];
NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
UIWindowScene *ws = (UIWindowScene *)array[0];
UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] init];
geometryPreferences.interfaceOrientations = UIInterfaceOrientationMaskPortrait;
[self updateVerticalSubviews];
[ws requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:^(NSError * _Nonnull error) {
//业务代码
}];
} else {
[[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationPortrait] forKey:@"orientation"];
}
}
}
网友评论