我需要在iPhone旋转时对层进行调整,但只有在从纵向旋转到横向时,或者反之亦然。如果将其从landscapeLeft 180度旋转到landscapeRight或从180度纵向旋转到portraitUpsideDown,则无需执行任何操作。UIViewController在哪里保持fromInterfaceOrientation信息?
所以为了避免不必要的动作,我像这样运行测试(self
是视图控制器):
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
BOOL bFromPortrait = UIInterfaceOrientationIsPortrait(self.interfaceOrientation);
BOOL bToPortrait = UIInterfaceOrientationIsPortrait(interfaceOrientation);
if (bFromPortrait != bToPortrait)
// make adjustments
}
不工作,因为时间willAnimate
被调用时,self.interfaceOrientation
早已已被改变以反映新的方向,所以bFromPortrait和bToPortrait总是相同的,调整从未发生过。
所以我试图运行从willRotate
这个代码,此时的“interfaceOrientation属性仍包含视图的原始方向。”但在这一点上层的主视图的自动调整大小调整尚未作出,所以我也没办法访问它将用于目标方向的自动调整大小的框架,否则我无法进行调整。
所以我为了添加一个布尔属性bFromPortrait
跟踪哪个方向的iPhone来自何处,将其设置在willRotate
,并在willAnimate
访问它(作品):
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
self.bFromPortrait = UIInterfaceOrientationIsPortrait(self.interfaceOrientation);
}
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
BOOL bToPortrait = UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
if (self.bFromPortrait != bToPortrait)
// Make adjustments.
}
但是我注意到如果我忽略didRotateFromInterfaceOrientation
,它的fromInterfaceOrientation
arg正确地反映了iPhone来自何处,尽管此方法在之后被称为。 因此,额外的布尔属性bFromPortrait
应该是不必要的; UIViewController已经在跟踪这些信息 - 但是在哪里?我如何访问它?
(您可能会问,为什么不在didRotateFromInterfaceOrientation
中进行调整。问题是此方法在动画之后运行,因此调整会弹出。为了将调整包含在动画中,它们必须发生在willAnimate
)
嗯,我不认为你正在处理如何区分90度和180度旋转的问题,但是你的代码确实提出了另一种跟踪方式:CGSize属性。新的宽度和高度仅与90度旋转时的缓存大小不同。肖像与风景可以由宽度/高度比决定。但我认为布尔变得更简单。 – Wienke 2012-04-21 15:35:13
@Wienke酷。你最终如何解决这个问题? – haroldcampbell 2012-05-08 03:22:36
问题的第二个代码示例解决了让事情起作用的主要问题,作为解决方法。我所问的小问题就是为什么这种解决方法应该是必要的。该信息已被UIViewController保存,所以它能很好地直接访问它。 – Wienke 2012-05-10 14:06:44