围绕其中心旋转UIImageView

问题描述 投票:0回答:1

我正在尝试将两个UIImage的中心旋转,但出现问题(您可以在下面的gif图像上进行检查),旋转很好,但是图像在上下摆动,有人知道它可以是什么吗?

Loader Animation

这是我用来旋转图像的代码

extension UIView {

private static let kRotationAnimationKey = "rotationAnimationKey"

func rotate(_ reversed: Bool = false, _ duration: Double = 1) {
    if layer.animation(forKey: UIView.kRotationAnimationKey) == nil {
        let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")
        rotationAnimation.fromValue = 0.0
        rotationAnimation.toValue = reversed ? -(Float.pi * 2.0) : Float.pi * 2.0
        rotationAnimation.duration = duration
        rotationAnimation.repeatCount = Float.infinity
        layer.add(rotationAnimation, forKey: UIView.kRotationAnimationKey)
    }
}

func stopRotating() {
    if layer.animation(forKey: UIView.kRotationAnimationKey) != nil {
        layer.removeAnimation(forKey: UIView.kRotationAnimationKey)
    }
}

谢谢大家!

ios swift rotation uiimageview
1个回答
0
投票

不要忘记设置边界]

layer.bounds = self.bounds
© www.soinside.com 2019 - 2024. All rights reserved.