viewWillTransitionToSize导致非旋转视图控

viewWillTransitionToSize导致非旋转视图控

本文介绍了viewWillTransitionToSize导致非旋转视图控制器调整大小并重新定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多人已经讨论了模仿本地iOS相机应用程序的技术(UI元素随设备旋转而在原地旋转).我实际上在.大多数人都让您锁定了UI的方向,但随后仅对您想要旋转的元素进行了强制转换.这可行,但是元素不会随您在本机iOS应用程序中看到的平滑动画而变化,并且会带来一些问题.具体来说,我的界面的一部分使用户可以在不离开该界面的情况下进行共享,但是当共享视图旋转时,它偏离中心.所以我想找到一种不同的方法.

A lot of people have discussed techniques for mimicking the native iOS camera app (where the UI elements pivot in-place as the device rotates). I actually asked a question about it before here. Most people have you lock the orientation of the UI, but then force a transformation on just the elements that you want to pivot. This works, but the elements don't pivot with the smooth animations you see in the native iOS app and it leads some issues. Specifically, part of my interface allows users to share without leaving this interface, but when the sharing view gets rotated, it comes out off-center. So I wanted to find a different way to do this.

我找到了 Apple的AVCam 的链接,从这里开始.我是新手,但我已经设法将其从Obj-C转换为Swift.以下是我当前正在使用的关键元素:

I found a link to Apple's AVCam sample, which got me off to a start here. I'm new to this stuff, but I managed to convert it from Obj-C to Swift already. Below is the key element of what I'm currently using:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

    coordinator.animateAlongsideTransition({ (UIViewControllerTransitionCoordinatorContext) -> Void in

        //Code here performs animations during the rotation

        let deltaTransform: CGAffineTransform = coordinator.targetTransform()
        let deltaAngle: CGFloat = atan2(deltaTransform.b, deltaTransform.a)
        var currentRotation: CGFloat = self.nonRotatingView.layer.valueForKeyPath("transform.rotation.z") as! CGFloat

        // Adding a small value to the rotation angle forces the animation to occur in a the desired direction, preventing an issue where the view would appear to rotate 2PI radians during a rotation from LandscapeRight -> LandscapeLeft.
        currentRotation += -1 * deltaAngle + 0.0001;
        self.nonRotatingView.layer.setValue(currentRotation, forKeyPath: "transform.rotation.z")

        }, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in
            print("rotation completed");
            // Code here will execute after the rotation has finished
            // Integralize the transform to undo the extra 0.0001 added to the rotation angle.
            var currentTransform: CGAffineTransform = self.nonRotatingView.transform
            currentTransform.a = round(currentTransform.a)
            currentTransform.b = round(currentTransform.b)
            currentTransform.c = round(currentTransform.c)
            currentTransform.d = round(currentTransform.d)
            self.nonRotatingView.transform = currentTransform
    })

    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
}

然后我有单独的视图控制器用于图标,它们在相反的方向上执行相同的转换.图标实际上现在已经正确地旋转到位(具有流畅的动画和所有内容),并且相机预览保持正确的方向.

I then have separate view controllers for icons that do the same transformation in the opposite direction. The icons actually pivot in place properly now (with smooth animations and everything) and the camera preview stays properly oriented.

问题是,当我将设备从纵向旋转到横向或反之时,非旋转视图会调整大小,并且所有内容都会放错位置.我该如何解决?

The problem is, the non-rotating view gets resized and everything gets misplaced when I rotate the device from portrait to landscape or vice-versa. How do I fix that?

推荐答案

我开始工作了(尽管花了我很多时间而不是应该的).我需要设置非旋转视图的width和height参数以在构建时删除,然后将以下内容添加到viewDidLoad():

I got it working (though it took me way more time than it should have). I needed to set the width and height parameters of the non-rotating view to remove at build time and then added the following to viewDidLoad():

let nonRotatingWidth = nonRotatingView.widthAnchor.constraintEqualToConstant(UIScreen.mainScreen().bounds.width)
let nonRotatingHeight = nonRotatingView.heightAnchor.constraintEqualToConstant(UIScreen.mainScreen().bounds.height)
nonRotatingView.addConstraints([nonRotatingWidth, nonRotatingHeight])

这使用了新的Swift 2约束语法,该语法相对简洁并且易于阅读.

This uses the new Swift 2 constraint syntax, which is relatively concise and easy to read.

我也只修改了新的约束语法(不进行任何转换)以通过updateViewConstraints()实现相同类型的接口(请参见).我相信这种方法是创建此类接口的一种更干净的方法,但是我尚未能够使其在运行时不崩溃的情况下正常工作.

I've also tinkered with only using the new constraint syntax (and no transformations) to achieve the same type of interface via updateViewConstraints() (see my question about it here). I believe that approach is a cleaner way to create this type of interface, but I have not yet been able to make it work without crashing at runtime.

这篇关于viewWillTransitionToSize导致非旋转视图控制器调整大小并重新定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 20:47