简体   繁体   English

在设备旋转时更改视图控制器

[英]Change view controller on device rotation

I am working on my first Xcode project and I am wondering if it is possible to have 2 view controllers, one which will have a portrait view and the second that will have horizontal. 我正在研究我的第一个Xcode项目,我想知道是否可以有2个视图控制器,一个具有纵向视图,第二个具有水平视图。 Is there any way I can link these 2 view controllers up so that when the device is rotated the view will change? 有什么方法可以将这两个视图控制器连接起来,这样当设备旋转时,视图会发生变化吗?

In viewDidLoad, add the rotated view to the superview and register an observer to UIDeviceOrientationDidChangeNotification with an object:[UIDevice currentDevice]. 在viewDidLoad中,将旋转的视图添加到superview,并使用对象向UIDeviceOrientationDidChangeNotification注册观察者:[UIDevice currentDevice]。 On the selector set its frame. 在选择器上设置其框架。 Then rotate the new rotated view, and set the old view's alpha to 0.0 然后旋转新的旋转视图,并将旧视图的alpha设置为0.0

- (void)deviceOrientationChangedNotification:(NSNotification *)notification 
{
     UIDevice *device = [notification object];
     UIDeviceOrientation orientation = device.orientation;

     if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) 
     {
        [rotatedView setTransform:CGAffineTransformMakeRotation(M_PI_2)];
        rotatedView.frame = [UIScreen mainScreen].bounds;
        oldView.alpha = 0.0;
     }
}

I don't recommend you to do that, because there are some intuitives UI controllers that apple has designed to manage the navigation of the app, you can't let the users to guess how to do some tasks in your app, you have to show them exactly how to do it. 我不建议你这样做,因为有一些直观的UI控制器,苹果设计用来管理应用程序的导航,你不能让用户猜测如何在你的应用程序中做一些任务,你必须向他们展示如何做到这一点。 So, rotate the device to go to another view controller, as you said :I want a completely different view when it is rotated... Is not a good idea. 所以,旋转设备转到另一个视图控制器,如你所说:旋转时我想要一个完全不同的视图...不是一个好主意。

Read the complete iOS Human Interface Guidelines to see what's recommended and what's not: 阅读完整的iOS人机界面指南,了解推荐内容和不推荐内容:

iOS Human Interface Guidelines iOS人机界面指南

In particular, read: Adaptivity and Layout : 特别是,阅读: 适应性和布局

Avoid gratuitous changes in layout. 避免布局中的无端更改。 A comparable experience in all environments lets people maintain their usage patterns when they rotate a device or run your app on a different device. 所有环境中的类似体验都可让人们在旋转设备或在不同设备上运行应用时保持其使用模式。 For example, if you use a grid to display images in a horizontally regular environment, you don't have to display the same information in a list in a horizontally compact environment, although you might adjust the dimensions of the grid. 例如,如果使用网格在水平常规环境中显示图像,则不必在水平紧凑的环境中的列表中显示相同的信息,尽管您可以调整网格的尺寸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM