iphone - 子视图旋转问题
我有一个 UIView 根和一个 UIView A 作为其子视图
我不允许根旋转,所以我在根类中编写,
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return NO;}
但我允许旋转根的子视图(A),所以在 A 类中,我写
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;}
但是它似乎 root 及其子视图 A 都无法旋转。
我应该如何允许 A 旋转但 root 不旋转?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
视图不响应shouldAutorotateToInterfaceOrientation,视图控制器则响应。如果您的视图控制器说它将旋转,那么它的视图和所有子视图都会旋转。如果你的视图控制器说它不会旋转,那么它的视图不会旋转,它的子视图也不会旋转。
有两种可能的方法来解决您的问题,具体取决于您想要做什么:
UIApplication
提供了一种将关键窗口作为 UIWindow 对象获取的方法,这可能是您想要的第一个想法。 UIWindow 继承自 UIView,因此添加和排列子视图的所有标准机制都适用。对于后者,您需要在相关视图上设置转换。从超级视图获取变换,获取逆并将其设置在视图上。有关更多详细信息,请参阅 CGAffineTransform 。所提到的 CoreAnimation 问题是,通过最短路线(正如 CoreAnimation 将采取的那样)从一个旋转到一个 180 度的过渡提供了两种可能的解决方案 - 一种是顺时针旋转,一种是逆时针旋转。在实践中,您需要在纵向模式下为视图的变换添加不可见的偏差,以确保 CoreAnimation 在您在纵向和上下颠倒之间旋转时选择正确的偏差。如果您使用的是 iPhone,那么在该设备上不支持 UIInterfaceOrientationPortraitUpsideDown 是完全可以接受的(大多数 Apple 应用程序都不支持),因此实际上不支持该方向可能更容易。
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:
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.