OGeek|极客世界-中国程序员成长平台

标题: ios - viewWillTransitionToSize 导致非旋转 View Controller 调整大小和重新定位 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:36
标题: ios - viewWillTransitionToSize 导致非旋转 View Controller 调整大小和重新定位

很多人都讨论过模仿原生 iOS 相机应用的技术(其中 UI 元素会随着设备旋转而原地旋转)。我实际上在 here 之前问过一个问题.大多数人让你锁定 UI 的方向,然后只对要旋转的元素强制进行转换。这可行,但元素不会随着您在 native iOS 应用程序中看到的流畅动画而旋转,并且会导致一些问题。具体来说,我的部分界面允许用户在不离开此界面的情况下进行共享,但是当共享 View 旋转时,它会偏离中心。所以我想找到一种不同的方法来做到这一点。

我找到了 Apple's AVCam 的链接 sample ,这让我从这里开始。我是新手,但我已经设法将它从 Obj-C 转换为 Swift。以下是我目前使用的关键元素:

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)
}

然后我有单独的 View Controller ,用于在相反方向进行相同转换的图标。图标现在实际上正确地旋转到位(具有流畅的动画和所有内容),并且相机预览保持正确的方向。

问题是,当我将设备从纵向旋转到横向(反之亦然)时,非旋转 View 会调整大小,并且所有内容都会错位。我该如何解决?



Best Answer-推荐答案


我得到了它的工作(虽然我花了比它应该花更多的时间)。我需要设置非旋转 View 的宽度和高度参数以在构建时删除,然后将以下内容添加到 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 约束语法,比较简洁易读。

我还修改了仅使用新的约束语法(并且没有转换)通过 updateViewConstraints() 实现相同类型的接口(interface)(参见 my question about it here )。我相信这种方法是创建这种类型接口(interface)的一种更简洁的方法,但我还不能让它在运行时不崩溃。

关于ios - viewWillTransitionToSize 导致非旋转 View Controller 调整大小和重新定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33132977/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4