【问题标题】:How do I detect a rotation on the iPhone without the device autorotating?如何在没有设备自动旋转的情况下检测 iPhone 上的旋转?
【发布时间】:2010-11-03 00:03:15
【问题描述】:

有人知道怎么做吗?

我是这么想的:

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

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
}

- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}

- (void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
}

- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration {
}

可能会阻止设备旋转(覆盖我的 UIViewController 的所有旋转方法而不调用超类),但我担心实际执行旋转的不是 UIViewController。

我的 UIViewController 在 UINavigationController 中。

有人有什么想法吗?

干杯, 尼克。

【问题讨论】:

  • 您实际上不需要覆盖所有那些空的委托方法,只需要覆盖您实际需要使用的那些。调用者应在调用之前测试您的子类是否响应特定方法,无论您是否定义它。这就是代表的工作方式。

标签: iphone cocoa-touch rotation


【解决方案1】:

你从shouldAutorotateToInterfaceOrientation: 返回YES,我怀疑你不是故意的。这是防止设备旋转的唯一方法。

至于获取设备当前方向,可以使用[[UIDevice currentDevice] orientation]

【讨论】:

  • 您好 - 感谢您的建议! [[UIDevice currentDevice]orientation] 将获得当前方向 - 但我需要检测何时发生这种情况而没有手机自动旋转。如果我从 shouldAutorotateToInterfaceOrientation 返回 NO,则不会调用任何委托函数,因此我失去了检测何时发生旋转的能力。还有其他人有什么想法吗?干杯,尼克。
【解决方案2】:

您可以注册通知UIDeviceOrientationDidChangeNotification(来自UIDevice.h),然后当您关心方向变化时调用此方法:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

当你不再关心方向变化时,调用这个方法:

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

通知将在适用时发送,您可以查看[UIDevice currentDevice].orientation 以了解当前方向。

【讨论】:

  • 谢谢 - 这正是我所追求的!
  • 不好的副作用是设备不会再进入睡眠状态了。
【解决方案3】:

附带说明 shouldAutoRotateToInterfaceOrientation 曾经在 2.x 中调用旋转时间,但在 3.0 中它的一致性要差得多。另一篇文章中提到的通知是获得可靠轮换指示的方法。

【讨论】:

    猜你喜欢
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    相关资源
    最近更新 更多