设备向上/向下倾斜和侧向触发方向通知 [英] Device tilting up/down and sideways triggers orientation notification

查看:89
本文介绍了设备向上/向下倾斜和侧向触发方向通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:即使是我实际想避免的小倾斜或震动也会被调用。

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)

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

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.

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

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.

处理轮换:

在iOS8之前,覆盖
didRotateFromInterfaceOrientation
在您的ViewController中。当设备旋转时,视图控制器会收到
。您当时处理轮换。

Pre-iOS8, override didRotateFromInterfaceOrientation in your ViewController. Your view controller receives this when the device rotates. You handle the rotation at that time.

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

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.

这篇关于设备向上/向下倾斜和侧向触发方向通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆