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.
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.