[英]iOS reset device orientation
考虑以下情况:iPhone处于纵向模式。 我这样做:
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[UIViewController attemptRotationToDeviceOrientation];
并将我的设备方向锁定为横向模式。 因此,我可以向任何方向转动手机,并且方向保持不变。 大;
然后我像这样解锁方向:
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[UIViewController attemptRotationToDeviceOrientation];
设备方向将根据需要遵循iPhone的物理方向。
现在问题仅在这种情况下出现:我将方向设置为横向( UIInterfaceOrientationLandscapeRight
)并保持手机垂直(在纵向模式下)。 屏幕根据需要旋转。 然后,我将方向设置回UIInterfaceOrientationUnknown
而不移动手机...屏幕一直停留在风景中,直到我将手机水平移动然后又回到垂直位置为止。 因此,除了解锁方向外,我还需要告诉iPhone,将屏幕方向更新为设备物理方向(例如,在Android中会自动发生)。 这可能吗? 最后:至少在iPhone模拟器上会发生这种情况。 我目前还没有真正可以测试的设备。
您需要订阅方向更改,并在需要时恢复方向。 这是完整的示例:
@interface ViewController ()
@property (nonatomic, assign) BOOL fixedOrientation;
@property (nonatomic, assign) UIDeviceOrientation lastOrientation;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.fixedOrientation = false;
[UIDevice.currentDevice beginGeneratingDeviceOrientationNotifications];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(deviceOrientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)deviceOrientationChanged:(NSNotification *)notif {
self.lastOrientation = [UIDevice.currentDevice orientation];
}
- (IBAction)buttPressed {
UIDeviceOrientation last = [UIDevice.currentDevice orientation];
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[UIViewController attemptRotationToDeviceOrientation];
self.fixedOrientation = true;
self.lastOrientation = last;
}
- (IBAction)butt2Pressed:(id)sender {
self.fixedOrientation = false;
NSNumber *value = [NSNumber numberWithInt:self.lastOrientation];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[UIViewController attemptRotationToDeviceOrientation];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
if (self.fixedOrientation) {
return UIInterfaceOrientationMaskLandscapeRight;
}
return UIInterfaceOrientationMaskAll;
}
@end
编辑: requires fullscreen
同时为iPhone和iPad选择requires fullscreen
选项才能正常工作,否则deviceOrientationChanged
将永远不会被调用。
我在Swift中有以下示例。 我发现使用UIApplication.shared.statusBarOrientation
更加可靠,而不是使用UIDeviceOrientation
。 我在GitHub项目中有更详细的示例
宣布
var currentInterfaceOrientation: UIInterfaceOrientation = UIInterfaceOrientation.unknown
更新方向
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: { _ in
if UIApplication.shared.statusBarOrientation.isLandscape {
self.currentInterfaceOrientation = UIInterfaceOrientation.landscapeLeft
} else {
self.currentInterfaceOrientation = UIInterfaceOrientation.portrait
}
self.deviceDidRotate()
})
}
private func deviceDidRotate() {
//perform your action
self.collectionView.reloadData()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.