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

时间:2014-10-31 07:53:47

标签: ios autorotate

我有一个iOS7的应用程序构建,它带有一个UIViewController,它应该支持横向左右和纵向 - 纵向颠倒,而其他ViewControllers应该只支持横向左右方向。我已使用通知通知方向更改并相应地刷新子视图。我也在检查UIDevice当前的旋转方向。我面临的问题是,即使轻微的摇晃或倾斜也会触发方向更改方法,刷新视图。我甚至在那里和那里都有奇怪的视野。我分别在viewDidLoad和viewWillDisappear上使用 beginGeneratingDeviceOrientationNotifications endGeneratingDeviceOrientationNotifications 。有什么办法可以限制设备轮换吗?

[[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];
}

如何在轻微摇晃或倾斜时跳过通知触发器。我实际上有一个关于方向改变的视图刷新动画,它给出了一种闪烁的效果和它的频繁。我需要删除它。请帮帮我们。

1 个答案:

答案 0 :(得分:1)

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

处理视图控制器旋转时,您无法使用[UIDevice currentDevice] .orientation中的设备旋转 - 它可以为您提供设备的瞬时方向。它不一定与接口方向匹配,并且可以快速来回切换。

接口方向由响应者链处理。 UIViewControllers在更改时接收消息。这些信息在方向稳定时发生,并且不会快速波动。

处理轮换:

  • 首先,在iOS 8文档中,查看"处理视图轮播"在 UIViewController

  • Pre-iOS8,覆盖 didRotateFromInterfaceOrientation 在您的ViewController中。您的视图控制器收到此信息 当设备旋转时你当时处理轮换。

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

或者,如果您已完全设置自动布局限制,则可以完全避免轮换处理。请参阅(http://annabd351.github.io/SquareCropViewController)以获取视图控制器的一个很好的示例,该控制器几乎没有代码处理一些棘手的旋转场景。

相关问题