iphone - 子视图旋转问题

发布于 2024-10-05 22:57:29 字数 443 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

‘画卷フ 2024-10-12 22:57:29

视图不响应shouldAutorotateToInterfaceOrientation,视图控制器则响应。如果您的视图控制器说它将旋转,那么它的视图和所有子视图都会旋转。如果你的视图控制器说它不会旋转,那么它的视图不会旋转,它的子视图也不会旋转。

有两种可能的方法来解决您的问题,具体取决于您想要做什么:

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

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:

  • 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文