如何快速创建圆形的运动动画

时间:2021-02-05 23:41:24

标签: swift xcode

所以我的问题是,我想要一个圆形的运动动画,它从左到右移动。圆是这样创建的:

let point = CGPoint.init(x: 10, y: 10)
let circlePath = UIBezierPath(arcCenter: point, radius: CGFloat(20), startAngle: CGFloat(0), endAngle: CGFloat(Double.pi * 2), clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = circlePath.cgPath

如何创建这样的圆的运动动画?

1 个答案:

答案 0 :(得分:2)

我已经在 imageView 上完成了移动动画,您可以在任何可以工作的 View 上尝试。

  private func animate(_ image: UIImageView) {
        
        UIView.animate(withDuration: 1, delay: 0, options: .curveLinear, animations: {
            image.transform = CGAffineTransform(translationX: self.view.frame.width/2-10, y: 0)
        }) { (success: Bool) in
            image.transform = CGAffineTransform.identity
            self.animate(image) // imageAnime is outlet
            self.imageAnim.layer.removeAllAnimations() // if you want to remove animation after its done animating once
        }
    }

在你想要完成动画的地方调用这个函数