ios - viewWillTransitionToSize 导致非旋转 View Controller 调整大小和重新定位
<p><p>很多人都讨论过模仿原生 iOS 相机应用的技术(其中 UI 元素会随着设备旋转而原地旋转)。我实际上在 <a href="https://stackoverflow.com/questions/33027948/need-help-setting-up-an-interface-where-rotation-is-fixed-for-most-elements-but/33045519#33045519" rel="noreferrer noopener nofollow">here</a> 之前问过一个问题.大多数人让你锁定 UI 的方向,然后只对要旋转的元素强制进行转换。这可行,但元素不会随着您在 nativeiOS 应用程序中看到的流畅动画而旋转,并且会导致一些问题。具体来说,我的部分界面允许用户在不离开此界面的情况下进行共享,但是当共享 View 旋转时,它会偏离中心。所以我想找到一种不同的方法来做到这一点。</p>
<p>我找到了 <a href="https://developer.apple.com/library/ios/samplecode/AVCam/" rel="noreferrer noopener nofollow">Apple's AVCam</a> 的链接 sample ,这让我从这里开始。我是新手,但我已经设法将它从 Obj-C 转换为 Swift。以下是我目前使用的关键元素:</p>
<pre><code>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)
}
</code></pre>
<p>然后我有单独的 ViewController ,用于在相反方向进行相同转换的图标。图标现在实际上正确地旋转到位(具有流畅的动画和所有内容),并且相机预览保持正确的方向。</p>
<p>问题是,当我将设备从纵向旋转到横向(反之亦然)时,非旋转 View 会调整大小,并且所有内容都会错位。我该如何解决?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我得到了它的工作(虽然我花了比它应该花更多的时间)。我需要设置非旋转 View 的宽度和高度参数以在构建时删除,然后将以下内容添加到 viewDidLoad():</p>
<pre><code>let nonRotatingWidth = nonRotatingView.widthAnchor.constraintEqualToConstant(UIScreen.mainScreen().bounds.width)
let nonRotatingHeight = nonRotatingView.heightAnchor.constraintEqualToConstant(UIScreen.mainScreen().bounds.height)
nonRotatingView.addConstraints()
</code></pre>
<p>这里使用了新的 Swift 2 约束语法,比较简洁易读。</p>
<p>我还修改了仅使用新的约束语法(并且没有转换)通过 updateViewConstraints() 实现相同类型的接口(interface)(参见 <a href="https://stackoverflow.com/questions/33223558/" rel="noreferrer noopener nofollow">my question about it here</a> )。我相信这种方法是创建这种类型接口(interface)的一种更简洁的方法,但我还不能让它在运行时不崩溃。</p></p>
<p style="font-size: 20px;">关于ios - viewWillTransitionToSize 导致非旋转 ViewController 调整大小和重新定位,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/33132977/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/33132977/
</a>
</p>
页:
[1]