首页 > 解决方案 > iOS:通过 CATransform3DRotate 旋转时跳转 uiview

问题描述

我尝试使用自己的锚点旋转 90 度角的 uiview(高度和宽度为 100)。更改锚点后,我翻译我的视图,以便两个更改相互抵消。

当我使用 (Double.pi) 旋转视图时,它会按预期设置动画,但是当我更改为 (Double.pi/2) 时,视图会跳起来。

    testView.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
    testView.layer.transform = CATransform3DMakeTranslation(0, -50, 0)

    UIView.animate(withDuration: 2.3, delay: 0, options: .curveLinear, animations: { () -> Void in
        var allTransofrom = self.testView.layer.transform
        var c3d = CATransform3DIdentity
        c3d.m34 = 2.5 / (2000)
        let rot = CATransform3DRotate(c3d, CGFloat(Double.pi/2), 1, 0, 0)
        allTransofrom = CATransform3DConcat(allTransofrom, rot)
        self.testView.layer.transform = allTransofrom
    }, completion: {(finished) in
    } )

完整代码在这里

标签: iosswiftuiviewcatransform3d

解决方案


我能够自己找到解决方案,但我仍然不知道跳跃来自哪里。

我没有通过 CGAffineTransform.translatedBy 或 CATransform3DMakeTranslation 进行翻译,而是简单地调整了我的 NSLayoutConstraint。

testViewAnchors[0].constant = testViewAnchors[0].constant + CGFloat(50)

在我的情况下,“翻译”的动画不是必需的,但可以为 NSLayoutConstraints 设置动画,这应该不是问题。


推荐阅读