微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iOS 9中的强制视图控制器方向

我正在制作一个摄影应用程序,允许以肖像或风景拍摄照片.由于项目的要求,我不能让设备方向自动旋转,但确实需要支持旋转.

使用以下定位方法时:

override func shouldAutorotate() -> Bool {
    return true
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if self.orientation == .Landscape {
        return UIInterfaceOrientationMask.LandscapeRight
    } else {
        return UIInterfaceOrientationMask.Portrait
    }
}

override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
    if self.orientation == .Landscape {
        return UIInterfaceOrientation.LandscapeRight
    } else {
        return UIInterfaceOrientation.Portrait
    }
}

我可以在启动时正确设置旋转.通过更改方向值并调用UIViewController.attemptRotationToDeviceOrientation(),我能够支持旋转到新的所需接口.但是,此旋转仅在用户实际移动其设备时发生.我需要它自动发生.

我可以调用:UIDevice.currentDevice().setValue(targetorientation.rawValue,forKey:“orientation”)来强制更改,但这会导致其他副作用,因为UIDevice.currentDevice().orientation只返回从该点开始的setValue上. (而且非常脏)

有什么我想念的吗?我已经研究过关闭并启动一个新的视图控制器,但这有其他问题,例如在解除时立即出现UI故障并立即呈现新的视图控制器.

编辑:

以下方法对我不起作用:

> Trick preferredInterfaceOrientationForPresentation to fire on viewController change
> Forcing UIInterfaceOrientation changes on iPhone

编辑2:

对潜在解决方案的思考

>直接设置方向(使用setValue)并处理iOS 9上出现的所有副作用(不可接受)
>我可以使用当前解决方案并指示用户需要旋转设备.设备物理旋转后,UI会旋转,然后正确锁定到位. (差的UI)
>我可以找到一种强制刷新方向并在没有物理操作的情况下旋转的解决方案. (我在问什么,并寻找)
>手工完成所有工作.我可以纵向或横向锁定界面,并手动旋转和调整容器视图的大小.这是“脏”的,因为它放弃了所有大小类自动布局功能,并导致更重的代码.我试图避免这种情况.

解决方法

我以前遇到过这个问题,我自己.我能够通过简单的解决方法解决它(和 GPUImage).我的代码在Objective-C中,但我确定你将它转换为Swift没有问题.

我首先将项目支持的旋转设置为我希望支持的所有内容,然后覆盖相同的UIViewController方法

-(BOOL)shouldAutorotate { 
    return TRUE; 
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

这允许设备旋转但在纵向模式下将保持不变.然后开始观察设备旋转:

[[NSNotificationCenter defaultCenter] addobserver:self
                                         selector:@selector(adjustForRotation:)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil];

如果设备处于纵向模式或横向,则更新UI:

-(void)adjustForRotation:(NSNotification*)notification
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

    switch (orientation) {
        case UIDeviceOrientationLandscapeLeft:
        {
            // UPDATE UI
        }
            break;
        case UIDeviceOrientationLandscapeRight:
        {
            // UPDATE UI
        }
            break;
        default: // All other orientations - Portrait,Upside Down,UnkNown
        {
            // UPDATE UI
        }
            break;
    }
}

最后,GPUImage根据设备的方向渲染图像.

[_gpuImageStillCamera capturePhotoAsImageProcessedUpToFilter:last_f
                                              withCompletionHandler:^(UIImage *processedImage,NSError *error) { 
    // Process the processedImage 
}];

原文地址:https://www.jb51.cc/iOS/328533.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐