iPhone-子视图旋转问题 [英] iphone - subview rotation problem

查看:55
本文介绍了iPhone-子视图旋转问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIView根目录和一个UIView A作为其子视图

我不允许根旋转,所以我写根类

 -(BOOL)应该AutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {返回否;} 

但是我允许旋转根的子视图(A),所以我在A类中编写

 -(BOOL)应该AutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {返回是;} 

但是看来根及其子视图A都不能旋转.

我应该如何允许A旋转而不允许root旋转?

谢谢

解决方案

视图不会响应shouldAutorotateToInterfaceOrientation,而视图控制器则会这样做.如果您的视图控制器说它将旋转,则其视图和所有子视图将旋转.如果您的视图控制器说它不会旋转,那么它的视图将不会旋转,它的子视图也不会旋转.

有两种方法可以解决您的问题,具体取决于您要执行的操作:

  • 如果非旋转视图位于旋转的所有内容的后面,则可以将其从视图控制器中删除,然后在viewWillAppear上将其添加到视图控制器后面的主应用程序窗口中,然后在viewDidDisappear上再次将其删除:
  • 如果非旋转视图和旋转视图需要以某种更复杂的方式混合在一起,则可以捕获willAnimateRotationToInterfaceOrientation:duration:并在非旋转视图上设置合适的逆变换.因此,它先旋转然后再旋转.由于CoreAnimation的工作方式,您将需要一些技巧才能在某些180度旋转之间获得正确的行为.

UIApplication 提供了一种将键窗口获取为UIWindow对象的方法,这可能是您第一个想法想要的.UIWindow继承自UIView,因此适用于添加和安排子视图的所有标准机制.

对于后者,您需要在相关视图上设置转换.从超级视图获取变换,获取逆并将其设置在视图上.有关更多详细信息,请参见 CGAffineTransform .提到的CoreAnimation问题是,以最短的路线从一转到另一180度的过渡(如CoreAnimation所采取的)提供了两种可能的解决方案-一种是顺时针的,另一种是逆时针的.实际上,在纵向模式下,您需要在视图的变换中添加不可见的偏移,以确保CoreAnimation在纵向和纵向倒置之间旋转时选择正确的偏移.如果您使用的是iPhone,则可以在该设备上不支持 UIInterfaceOrientationPortraitUpsideDown (大多数Apple应用程序不支持),这是可以接受的,因此,实际上,不支持该方向可能更容易.

I have a UIView root and a UIView A as its subview

I don't allow root to be rotated, so I write in root class

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return NO;}

but I allow the root's subview (A) to be rotated, so in A class, I write

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;}

But it seems that both root and its subview A can't rotate.

How should I allow A to be rotated but root not?

thanks

解决方案

Views don't answer to shouldAutorotateToInterfaceOrientation, view controllers do. If your view controller says it will rotate then its view and all child views will rotate. If your view controller says it won't rotate then its view won't rotate and neither will its children.

There are two possible approaches to solving your problem, depending on exactly what you want to do:

  • if the non-rotating view is behind everything that rotates, you could remove it from the view controller and add it to the main application window behind the view controller upon viewWillAppear: and remove it again on viewDidDisappear:
  • if the non-rotating and rotating views need to be intermingled in some more complicated way, you can catch willAnimateRotationToInterfaceOrientation:duration: and set a suitable inverse transform on the non-rotating view. So its rotated and then unrotated. You'll need to be a bit tricky to get correct behaviour between certain 180 degree rotations because of the way CoreAnimation works.

UIApplication provides a means to get the key window as a UIWindow object, which is probably what you'd want for the first idea. UIWindow inherits from UIView, so all the standard mechanisms for adding and arranging subviews apply.

For the latter, you'll want to set the transform on the relevant view. Get the transform from the superview, get the inverse and set it on the view. See the CGAffineTransform for more specifics. The CoreAnimation issue alluded to is that the transition from one rotation to one 180 degrees different by the shortest route (as CoreAnimation will take) gives two potential solutions — one clockwise and one anticlockwise. In practice, you need to add an invisible bias to the view's transform when in portrait mode to ensure CoreAnimation picks the right one if you're rotating between portrait and portrait upside down. If you're on the iPhone, it's quite acceptable not to support UIInterfaceOrientationPortraitUpsideDown on that device (most of the Apple apps don't), so it's actually probably easier just not to support that orientation.

这篇关于iPhone-子视图旋转问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆