我有一个针对 iOS7 的应用程序构建,其中 UIViewController 应该支持横向左右和纵向 - 纵向上下颠倒,其他 ViewController 应该仅支持横向左右方向。我已使用通知来通知方向更改并相应地刷新子视图。我还在检查 UIDevice 当前的旋转方向。这我面临的问题是,即使是轻微的摇晃或倾斜也会触发方向改变方法这刷新了视图。我什至当时就得到了奇怪的视图框架。我用开始生成设备方向通知 and 结束生成设备方向通知分别在 viewDidLoad 和 viewWillDisappear 上。有什么方法可以限制仅设备旋转吗?
[[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];
}
如何在轻微摇动或倾斜时跳过通知触发。实际上,我在方向更改时的视图刷新上有一个动画,它会产生闪烁的效果及其频繁。我需要删除这个。请大家帮忙。
(比我的评论更完整的答案)
处理视图控制器旋转时,不能使用device来自 [UIDevice currentDevice].orientation 的旋转——为您提供设备的瞬时方向。它不一定匹配界面方向,并且可以快速来回改变。
The 界面方向由响应者链处理。 UIViewController 在发生变化时会收到消息。这些消息在方向稳定时发生,并且不会快速波动。
处理旋转:
或者,如果您完全设置了自动布局约束,则可以完全避免处理旋转。看 (http://annabd351.github.io/SquareCropViewController)这是视图控制器的一个很好的例子,它几乎不需要任何代码就可以处理一些棘手的旋转场景。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)