当iPhone旋转90度时如何触发一个函数?

5

我如何检测iPhone的旋转大约90度?此外,我不需要/不想让屏幕本身旋转。我只想在手机旋转时调用一个函数。谢谢!

5个回答

8

你的视图控制器将被发送:

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

当手机旋转时,实现它以执行某些操作。

旋转完成后,您将得到:

  - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

这里是文档中的相关内容(参见处理视图旋转)。

http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html


1
当您旋转设备时,您的视图控制器将接收到一个didRotateFromInterfaceOrientation:消息,并带有旧方向。您可以检查interfaceOrientation属性以查看当前旋转情况,并相应地执行某些操作。
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

1

您需要捕获在视图控制器旋转之前发送的事件。具体来说:

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

和后旋转

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

1

你应该先实现实例方法 "shouldAutorotateToInterfaceOrientation" 并返回 YES。 然后这两个方法将被调用。


0

方法名称是正确的...但它们从未被调用过。这是什么原因呢?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接