iPhone:屏幕旋转然后应用程序崩溃

时间:2011-02-02 14:21:25

标签: iphone orientation duration

我希望在定向时获得即时屏幕的旋转。在我的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];

在代码行尝试将持续时间设置为零时,应用程序崩溃之后旋转完成。

知道我做错了吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

你正在创造一个无限循环:

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

你从内部调用相同的函数。你打算做什么?

答案 1 :(得分:1)

你正在堆叠许多willRotateToInterfaceOrientation:duration:一个接一个地调用。 事实上,第一次调用该方法时,它将再次调用它,并再次调用它,直到崩溃。 放置持续时间:0表示什么都没有,实际上用于旋转效果的CoreAnimation时间是在一个单独的线程上运行,而不是运行循环,因此您无法以这种方式停止堆叠调用。 应用程序在轮换后崩溃的原因可能是因为当堆栈被填满时你的动画已经开始(单独的线程)。

您只需禁用自动旋转并观察UIDeviceOrientationDidChangeNotification通知即可获得即时定位,然后应用适当的视图转换。