0

我想在定向时获得即时屏幕的旋转。在我的 ViewController 中有一个 UIImage 和一个 UILabel。我这样做:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[self willRotateToInterfaceOrientation:[self interfaceOrientation] duration:0];
}

没有行应用程序不会崩溃:

[self willRotateToInterfaceOrientation:[self interfaceOrientation] duration:0];

随着代码行尝试将持续时间设置为零,应用程序在轮换完成后立即崩溃。

知道我做错了什么吗?

谢谢你。

4

2 回答 2

1

您正在创建一个无限循环:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
  [self willRotateToInterfaceOrientation:[self interfaceOrientation] duration:0];
  }

您正在从自身内部调用相同的函数。你打算做什么?

于 2011-02-02T14:38:20.960 回答
1

您正在堆叠许多 willRotateToInterfaceOrientation:duration: 调用一个接一个。事实上,第一次调用该方法时,它会一次又一次地调用它,一次又一次,......直到崩溃。放置 duration:0 没有任何意义,事实上用于旋转效果的 CoreAnimation 时间是在与 Run Loop 不同的线程上运行的,因此您无法停止以这种方式堆叠调用。应用程序在旋转后崩溃的原因可能是由于堆栈被填满时您的动画已经开始(单独的线程)。

您可以通过简单地禁用自动旋转并观察 UIDeviceOrientationDidChangeNotification 通知来获得即时方向,然后应用适当的视图转换。

于 2011-02-02T14:39:59.990 回答