我们可以手动创建UIDeviceOrientationDidChangeNotification事件吗?
在AppDelegate.swif中,我有
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask 此方法将根据用户设置返回结果。
它工作正常,但用户需要在应用程序运行过程中更改设置。
但设置会在用户旋转设备后生效,我想要的是让它立即生效
发布于 2018-03-04 17:12:34
使用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")
}
}发布于 2018-03-04 17:29:19
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];https://stackoverflow.com/questions/49093734
复制相似问题