如何使动画停在路径上的特定位置?

时间:2017-06-12 17:20:18

标签: ios swift core-animation cakeyframeanimation

我正在尝试制作动画,其中图像沿着圆形路径移动并停止。我可以让图像沿着路径行进,但是在让它停在特定点时遇到了问题。我尝试做关键帧值,但由于路径是圆形的,当我输入一个关键帧值时,它不会沿着路径移动而是沿着x或y轴移动(postion.x,position.y,position.xy)。在viewDidAppear中,我试图让图像移入并停在右上角的UIImageView,如下面的故事板所示: ImageView

按下按钮后,图像将继续沿着屏幕上的圆形路径移动。目前这里是代码:

  func moveMatchIn() {

    let circlePath = UIBezierPath(arcCenter: CGPoint(x:view.frame.width, y:view.frame.height/2), radius: CGFloat(200), startAngle: CGFloat(0), endAngle:CGFloat(CGFloat.pi * 2), clockwise: false).cgPath

    let animation = CAKeyframeAnimation(keyPath: "position")
    animation.calculationMode = kCAAnimationPaced
    animation.duration = 1
    animation.speed = 0.5
    animation.path = circlePath

    theyMatchImageView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
    theyMatchImageView.contentMode = .scaleAspectFill
    theyMatchImageView.image = matchImage
    theyMatchImageView.setRounded()

    view.addSubview(theyMatchImageView)
    theyMatchImageView.layer.add(animation, forKey: "position")

    // circleLayer is only used to locate the circle animation path
    let circleLayer = CAShapeLayer()
    circleLayer.path = circlePath
    circleLayer.strokeColor = UIColor.clear.cgColor
    circleLayer.fillColor = UIColor.clear.cgColor
    view.layer.addSublayer(circleLayer)
  }

0 个答案:

没有答案