简体   繁体   English

iPhone如何响应设备旋转显示不同的视图控制器?

[英]iPhone how to display a different view controller in response to device rotation?

I've noticed that most consumer-friendly Android and iPhone fitness apps have two interface modes - in portrait mode the user gets more detailed information, but when the user turns the device to landscape mode, a full screen graph is added to cover the entire screen. 我注意到,大多数对消费者友好的Android和iPhone健身应用程序都有两种界面模式-在纵向模式下,用户可以获得更详细的信息,但是当用户将设备转到横向模式时,会添加全屏图形以覆盖整个屏幕屏幕。

I'm interested in how to implement transition to a different view controller in response to device rotation on iPhone. 我对如何实现过渡到其他视图控制器以响应iPhone上的设备旋转感兴趣。 My initial thoughts are to intercept (willRotateToInterfaceOrientation event, then get the app delegate and add a full screen graph view controller to the window). 我最初的想法是进行拦截(willRotateToInterfaceOrientation事件,然后获取应用程序委托并将全屏图形视图控制器添加到窗口中)。

Is there a better way of turning an iPhone rotation into a transition to another view controller? 有没有更好的方法可以将iPhone旋转转换为到另一个视图控制器的过渡? Like hiding the status bar and pushing a modal view controller in landscape mode with animation? 就像隐藏状态栏并在带有动画的横向模式下推动模式视图控制器一样?

First ask yourself whether you really need a separate view controller. 首先,问问自己是否真的需要一个单独的视图控制器。 One view controller can easily hide or unhide a graph. 一个视图控制器可以轻松隐藏或取消隐藏图形。 If this graph needs its own view conroller then you could use a container view that contains the graph which refers to its own view conroller. 如果此图需要其自己的视图控制器,则可以使用包含该图的容器视图,该图引用其自己的视图控制器。 That is what container views are made for. 那就是做容器视图的目的。 The "Master" view controller then would just hide and unhide the container view in response to rotation events (and layout them accordingly etc. pp.) 然后,“主”视图控制器将只响应旋转事件来隐藏和取消隐藏容器视图(并相应地对其进行布局等。

If you prefer to add or remove the container view from its super view (most probably self.view from the "Master" view controller's point of view) then do that instead of hiding and unhiding. 如果您希望从其超级视图中添加或删除容器视图(最有可能是从“主”视图控制器的角度来看为self.view),则可以这样做,而不是隐藏和取消隐藏。 That is probably most appropriate. 那可能是最合适的。

The upside of this appoach would be that it works regardless of the navigaiton structure you are in, regardless of whether the rotated view controller was pushed or presented modally, regardless of whether you are in a tab bar driven app or a single view app, whether you are using storyboard, works with IB as well as programmatically, etc. pp. 这种方法的好处在于,无论您使用的是哪种导航结构,它都可以工作,而无论旋转视图控制器是按模式推还是按模式显示,无论您是在标签栏驱动的应用程序中还是在单个视图应用程序中,您正在使用情节提要,与IB以及以编程方式一起使用等。

There is nothing wrong with fetching the window instance from the app's delegate. 从应用程序的委托中获取窗口实例没有错。 I just don't see the need for doing so. 我只是没有看到这样做的必要。 Seems rather complicated to me compared to the alternatives. 与替代方案相比,对我来说似乎相当复杂。

The willRotateToInterfaceOrientation method works well. willRotateToInterfaceOrientation方法效果很好。

In addition to switching views, two other useful things you might want to do in there are: 除了切换视图之外,您可能还想做另外两件事:

1) Hide/Show the status bar. 1)隐藏/显示状态栏。 (I like to hide it in landscape) (我喜欢将其隐藏在风景中)

[[UIApplication sharedApplication] setStatusBarHidden:UIInterfaceOrientationIsLandscape(toInterfaceOrientation)  withAnimation:UIStatusBarAnimationSlide];

2) Hide/Show any UINavigationBar. 2)隐藏/显示任何UINavigationBar。 (Maybe your landscape view will benefit from the extra height) (也许您的风景将受益于额外的高度)

[self.navigationController setNavigationBarHidden:UIInterfaceOrientationIsLandscape(toInterfaceOrientation) animated:YES];

You could have one view controller that has the willRotateToInterfaceOrientation method, and that viewcontroller has two other viewcontrollers as variables. 您可能有一个具有willRotateToInterfaceOrientation方法的视图控制器,而该视图控制器还有另外两个视图控制器作为变量。

Once the device rotates, you switch the viewcontrollers' views (very crude code example:) 设备旋转后,即可切换视图控制器的视图(非常原始的代码示例:)

-(void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {

if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight)) {
    [self.secondViewController.view removeFromSuperView];
    self.firstViewController.view.frame = self.bounds;
    [self.view addSubView:self.firstViewController.view];
  } else {
    [self.firstViewController.view removeFromSuperView];
    self.secondViewController.view.frame = self.bounds;
    [self.view addSubView:self.secondViewController.view];
  }
}

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

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