我们可以手动创建UIDeviceOrientationDidChangeNotification事件吗?
在AppDelegate.swift我有
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
此方法将返回结果取决于用户设置。
它运行良好但用户需要在应用程序运行期间更改设置。
但是用户旋转设备后设置会生效,我想要的是让它立即生效
使用NotificationCenter
-
setupRotationListener()
/// Setup rotation listener
func setupRotationListener() -> Void {
NotificationCenter.default.addObserver(self, selector: #selector(self.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}
/// Remove rotationListener
func removeRotationlistener() -> Void {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}
/// Rotate camera
func rotated() {
//do your task
if UIDevice.current.orientation.isLandscape {
print("Landscape")
} else {
print("Portrait")
}
}
I found a solution from stackoverflow
他的解决方案真的解决了我的问题:
在iOS <= 4和iOS> = 6中:
UIViewController *vc = [[UIViewController alloc]init];
[self presentModalViewController:vc animated:NO];
[self dismissModalViewControllerAnimated:NO];
[vc release];
在iOS 5中:
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *view = [window.subviews objectAtIndex:0];
[view removeFromSuperview];
[window addSubview:view];