Failure notification listeners

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]; 
+4
source share
2 answers

I assume that you add observers again and never delete them. This means that the same entry point can be called multiple times for a single event. It also means that when the self goes away, you get a failure.

+2
source

A notification is sent only if the device changes from one orientation to another - it does not work longer than necessary.

UIDeviceOrientation has 2 more orientations than UIInterfaceOrientation . It has FaceUp and FaceDown. They are probably the ones that start up, and you are not interested in this.

 typedef enum { UIDeviceOrientationUnknown, UIDeviceOrientationPortrait, UIDeviceOrientationPortraitUpsideDown, UIDeviceOrientationLandscapeLeft, UIDeviceOrientationLandscapeRight, UIDeviceOrientationFaceUp, UIDeviceOrientationFaceDown } UIDeviceOrientation; 

You might prefer to listen for UIInterfaceOrientation changes in the view controller that is currently on the screen.

 typedef enum { UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait, UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight, UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft } UIInterfaceOrientation; 

.

 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
+1
source

Source: https://habr.com/ru/post/1391343/


All Articles