I use the following code to detect / listen when the iPad changes the orientation of the device.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
This calls my didRotate method: when something changes. In my didRotate method: I use UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; to find out what orientation is, and apply my code accordingly.
However, in real testing, I noticed that didRotate: receives a call every second if the iPad is in the user's hand. It seems that the listener is literally listening to every little tilt and shift in the iPad, which obviously happens in the hands of a person (as opposed to flat on a table).
Any ideas on how I can fix this? I could change my code to use the orientation of the interface, but for some reason I had problems with it. Thanks.
* UPDATE . This listener is created in my subclass of UIImageView. On the screen about a dozen or more. I added [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; to my delete methods when I delete an instance. This helps explain why didRotate showed so much (my mistake).
However, I narrowed down the emergencies: whenever I delete this instance of this subclass and rotate my iPad, I fail. Sometimes I get strange messages like [__NSArrayM didRotate]: this is an unrecognized selector (and other wierd objects like UIPanVelocity ... something). Is my listener still listening after the instance is deleted?
FIXED : Thank you for your help. Finally, I noticed what happened. I deleted the instance without deleting the observer and completing notifications. Adding the following code to instance removal methods fixes my problem:
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] removeObserver:self];
source share