繁体   English   中英

无限旋转视图 360 度

[英]Rotate a view 360 degrees infinitely

我正在使用此代码将视图无限旋转 360 度。 但我的观点并没有旋转:

func rotateImageView() {

        UIView.animate(withDuration: 3.0, delay: 0, options: [.repeat, .curveLinear], animations: {
            self.vinylView.transform = CGAffineTransform(rotationAngle: .pi * 2)
        })
        
    }

如何解决?

用 / 替换 pi *,删除重复,添加完成并调用函数,如下所示:

private func rotateImageView() {
    UIView.animate(withDuration: 3, delay: 0, options: .curveLinear, animations: {
        self.vinylView.transform = self.vinylView.transform.rotated(by: .pi / 2)
    }) { (finished) in
        if finished {
            self.rotateImageView()
        }
    }
}

使用 CABasicAnimation 的解决方案

// Rotate vinvlView
vinylView.layer.add(CABasicAnimation.rotation, forKey: nil)

extension CABasicAnimation {
    static let rotation : CABasicAnimation = {
        let animation = CABasicAnimation(keyPath: "transform.rotation.z")
        animation.repeatCount = .infinity // Rotate a view 360 degrees infinitely
        animation.fromValue = 0
        animation.toValue = CGFloat.pi * 2
        animation.duration = 3.0
        return animation
    }()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM