I have an application build for iOS7 with a UIViewController that should support landscape left-right and portrait - portrait upside down and other ViewControllers should support landscape left and right orientation alone. I have used the notification to notify the orientation change and refresh the subviews accordingly. I am also checking UIDevice's current orientation to rotate. The issue I am facing is that, even a slight shake or tilt triggers orientation change method which refreshes the views. I even get bizarre view frames then and there. I use beginGeneratingDeviceOrientationNotifications and endGeneratingDeviceOrientationNotifications on viewDidLoad and viewWillDisappear respectively. Is there any way I can restrict to device rotation alone?
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceDidRotate:) name:UIDeviceOrientationDidChangeNotification object:nil]; // adding observer to notify when device rotates.
deviceDidRotate: gets called even for a small tilt or shake which I actually want to avoid.
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
//Ignoring specific orientations
if (orientation == UIDeviceOrientationFaceUp||orientation == UIDeviceOrientationFaceDown ||orientation == UIDeviceOrientationUnknown)
{
return;
}
if ((UIDeviceOrientationIsPortrait(orientation) || UIDeviceOrientationIsLandscape(orientation))) {
[UIViewController attemptRotationToDeviceOrientation];
}
To avoid the tilt effect I am using this delegate method
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self handleViewArrangementWithCurrentOrientation];
}
How can I skip notification trigger on slight shake or tilt. I am actually having a animation on view refresh on orientation change which gives a flickering kind of effect and its frequent. I need to remove this. Please help guys.
(a more complete answer than my comment)
When handling view controller rotations, you can't use the device rotation from [UIDevice currentDevice].orientation -- that gives you the instantaneous orientation of the device. It doesn't necessarily match the interface orientation, and can change back and forth rapidly.
The interface orientation is handled by the responder chain. UIViewControllers receive messages when it changes. These messages happen when the orientation has stabilized, and will not fluctuate rapidly.
To handle rotation:
First, in iOS 8 docs, look at "Handling View Rotations" in UIViewController
Pre-iOS8, override didRotateFromInterfaceOrientation in your ViewController. Your view controller receives this when the device rotates. You handle the rotation at that time.
- Starting in iOS8, use the Transition Coordinator mechanism. Set your transition coordinator using the transitionCoordinator property of the view controller.
Or, you can avoid handling rotations altogether if you've set up your autolayout constraints completely. See (http://annabd351.github.io/SquareCropViewController) for a good example of a view controller which handles some tricky rotation scenarios with almost no code.
来源:https://stackoverflow.com/questions/26669799/device-tilting-up-down-and-sideways-triggers-orientation-notification