设备向上/向下倾斜和横向触发方向通知

我有一个iOS7的应用程序构build与UIViewController,应该支持风景左右和纵向肖像颠倒和其他ViewControllers应支持横向左右方向。 我已经使用通知来通知方向更改并相应刷新子视图。 我也检查UIDevice的当前方向旋转。 我面对问题是,即使轻微的摇晃或倾斜,也会触发更新观点的方向改变方法 。 我甚至在那里和那里得到奇怪的视图框架。 我对viewDidLoad和viewWillDisappear分别使用了beginGeneratingDeviceOrientationNotificationsendGeneratingDeviceOrientationNotifications 。 有什么办法可以限制到单独的设备旋转?

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceDidRotate:) name:UIDeviceOrientationDidChangeNotification object:nil]; // adding observer to notify when device rotates. 

deviceDidRotate:即使是我实际上想要避免的一个小的倾斜或摇晃。

 UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; //Ignoring specific orientations if (orientation == UIDeviceOrientationFaceUp||orientation == UIDeviceOrientationFaceDown ||orientation == UIDeviceOrientationUnknown) { return; } if ((UIDeviceOrientationIsPortrait(orientation) || UIDeviceOrientationIsLandscape(orientation))) { [UIViewController attemptRotationToDeviceOrientation]; } 

为了避免倾斜效应,我使用这种委托方法

 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { [self handleViewArrangementWithCurrentOrientation]; } 

如何轻轻摇动或倾斜跳过通知触发器。 我实际上是在视图刷新方向变化的animation,给出了一个闪烁的效果和它的频繁。 我需要删除这个。 请帮助家伙。

(比我的评论更完整的答案)

处理视图控制器旋转时,不能使用[UIDevice currentDevice] .orientation中的设备旋转 – 这会为您提供设备的即时方向。 它不一定匹配界面的方向,并且可以快速地来回切换。

界面方向由响应者链处理。 UIViewControllers在更改时接收消息。 这些消息发生在方向稳定后,不会迅速波动。

处理旋转:

  • 首先,在iOS 8文档中,查看UIViewController中的 “处理视图旋转”

  • 在iOS8之前,在您的ViewController中重写didRotateFromInterfaceOrientation 。 设备旋转时,您的视图控制器会收到此信息。 你当时处理轮换。

  • 从iOS8开始,使用转换协调器机制。 使用视图控制器的transitionCoordinator属性设置您的过渡协调器。

或者,如果完全设置了自动布局约束,则可以完全避免处理旋转。 查看( http://annabd351.github.io/SquareCropViewController )是一个视图控制器的一个很好的例子,它处理几乎没有代码的棘手的旋转场景。