iPhone:屏幕旋转然后应用程序崩溃
我想在定向时获得即时屏幕旋转。在我的 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];
尝试将持续时间设置为零的代码行应用程序会在旋转完成后立即崩溃。
知道我做错了什么吗?
谢谢。
I want to get an instant screen's rotation when orienting it. In my ViewController there is one UIImage and one UILabel. I do as follows:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[self willRotateToInterfaceOrientation:[self interfaceOrientation] duration:0];
}
Without the line app doesn't crash:
[self willRotateToInterfaceOrientation:[self interfaceOrientation] duration:0];
With the line of code trying to set duration to zero app crashes right after the rotation finishes.
Any idea what I'm doing wrong?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在一个接一个地堆叠许多 willRotateToInterfaceOrientation:duration: 调用。
事实上,第一次调用该方法时,它会一次又一次地调用它,直到崩溃。
设置持续时间:0 没有任何意义,事实上,用于旋转效果的 CoreAnimation 计时是在与运行循环线程不同的线程上运行的,因此您无法以这种方式停止堆栈调用。
应用程序在旋转后崩溃的原因可能是因为当堆栈被填满时,您的动画已经开始(单独的线程)。
您只需禁用自动旋转并观察 UIDeviceOrientationDidChangeNotification 通知即可获得即时方向,然后应用适当的视图变换。
You are stacking many willRotateToInterfaceOrientation:duration: calls one after the other.
Infact first time the method is called, it will call it again, and again, and again,... until crash.
Putting duration:0 means nothing, infact the CoreAnimation timing used for the rotation effect is run on a separate thread than the Run Loop one so you cannot stop stacking calls in this way.
The reason why the app crashes after rotation probably is due to the fact that while the stack get filled your animation already started (separate thread).
You can get instant orientation by simply disabling autorotation and observing for the UIDeviceOrientationDidChangeNotification notification, then apply the appropriate view transform.
您正在创建一个无限循环:
您正在从其内部调用相同的函数。你打算做什么?
You're creating an infinite loop:
You're calling the same function from within itself. What are you aiming to do?