Zev*_*erg 5 uiviewcontroller screen-rotation ios ios9
我有一个简单的应用程序,包含一个视图控制器.我从Xcode 7 GM单视图应用程序模板开始,但随后删除了主故事板,并设置了我的视图控制器,如下所示:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let vc = ViewController()
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window!.rootViewController = vc
window!.makeKeyAndVisible()
return true
}
Run Code Online (Sandbox Code Playgroud)
在我的信息plist中,我在支持的界面方向下指定了所有方向,并且应用程序在iPad上旋转到所有方向.
但是,在我的简单视图控制器中,永远不会调用shouldAutorotate()和supportedInterfaceOrientations()方法.这是一个问题,因为我正在尝试启用和禁用自动旋转的UI控件.什么可能阻止这些方法被调用?
这里的示例项目(需要Swift 2)
¹non-UINavigationController
根据Apple开发者论坛上的这篇文章,如果您启用了iPad多任务处理(iOS 9中的新增功能),则无法再控制您支持的方向:
https://forums.developer.apple.com/message/13508#13508
你可以添加计数器旋转,但它并不漂亮,至少就我所知.我得到它的工作,但是当你旋转时无法禁用角落的动画,所以你看到它看起来像旋转的奇怪的情况,但内容不旋转.
这是我用来对抗旋转的代码.请注意,我必须隐藏状态栏,否则它也会旋转,我无法弄清楚如何对付它.
另请注意,执行自动旋转self.navigationController.view.superview.superview可能不是最佳方式,并且可能在将来的某个时刻中断.可能有更好的方法来获得用于对抗旋转的正确视图.显然,如果您不使用导航控制器,则需要传入不同的视图.因人而异.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
self.startingInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self updateLayoutsForCurrentOrientation:toInterfaceOrientation view:self.navigationController.view.superview.superview];
}
- (void)updateLayoutsForCurrentOrientation:(UIInterfaceOrientation)toInterfaceOrientation view:(UIView *)view {
CGAffineTransform transform = CGAffineTransformIdentity;
if (self.startingInterfaceOrientation == UIInterfaceOrientationPortrait) {
switch (toInterfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
transform = CGAffineTransformMakeRotation(M_PI/2.0f);
break;
case UIInterfaceOrientationLandscapeRight:
transform = CGAffineTransformMakeRotation(-M_PI/2.0f);
break;
case UIInterfaceOrientationPortrait:
transform = CGAffineTransformIdentity;
break;
case UIInterfaceOrientationPortraitUpsideDown:
transform = CGAffineTransformMakeRotation(M_PI);
break;
default:
break;
}
}
else if (self.startingInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
switch (toInterfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
transform = CGAffineTransformMakeRotation(-M_PI/2.0f);
break;
case UIInterfaceOrientationLandscapeRight:
transform = CGAffineTransformMakeRotation(M_PI/2.0f);
break;
case UIInterfaceOrientationPortrait:
transform = CGAffineTransformMakeRotation(M_PI);
break;
case UIInterfaceOrientationPortraitUpsideDown:
transform = CGAffineTransformIdentity;
break;
default:
break;
}
}
else if (self.startingInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
switch (toInterfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
transform = CGAffineTransformIdentity;
break;
case UIInterfaceOrientationLandscapeRight:
transform = CGAffineTransformMakeRotation(M_PI);
break;
case UIInterfaceOrientationPortrait:
transform = CGAffineTransformMakeRotation(-M_PI/2.0f);
break;
case UIInterfaceOrientationPortraitUpsideDown:
transform = CGAffineTransformMakeRotation(M_PI/2.0f);
break;
default:
break;
}
}
else if (self.startingInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
switch (toInterfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
transform = CGAffineTransformMakeRotation(M_PI);
break;
case UIInterfaceOrientationLandscapeRight:
transform = CGAffineTransformIdentity;
break;
case UIInterfaceOrientationPortrait:
transform = CGAffineTransformMakeRotation(M_PI/2.0f);
break;
case UIInterfaceOrientationPortraitUpsideDown:
transform = CGAffineTransformMakeRotation(-M_PI/2.0f);
break;
default:
break;
}
}
view.transform = transform;
}
Run Code Online (Sandbox Code Playgroud)
大部分代码都是根据Jared Sinclair的JTSImageViewController(经过他的许可发布)改编而来,可以在github上的MIT许可下获得:https://github.com/jaredsinclair/JTSImageViewController
| 归档时间: |
|
| 查看次数: |
9319 次 |
| 最近记录: |