Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Move UIImage partly along path

I am using this code to move a UIImage along a curve:

    // paint curve for sun
    let path = UIBezierPath()

    let imageSunName = "small_sun.png"
    let imageSun = UIImage(named: imageSunName)
    let imageView = UIImageView(image: imageSun!)

    imageView.frame = CGRect(x: xStart, y: yStart, width: 24, height: 24)
    self.view.addSubview(imageView)

    path.moveToPoint(CGPoint(x: xStart,y: yStart))

    path.addQuadCurveToPoint(CGPoint(x: xEnd, y: yEnd), controlPoint: CGPoint(x: xMiddleTop, y: yMiddleTop))

    let animation = CAKeyframeAnimation(keyPath: "position")
    animation.path = path.CGPath

    animation.repeatCount = 0
    animation.duration = 5.0

    imageView.layer.addAnimation(animation, forKey: "animate position along path")

Nevertheless, two issues remain:

  • I would like to move the image only partly along the path (according to the current time vs sunrise/sunset) - how can this be done?
  • The image jumps to its origin position after the animation stopped - how can I avoid this?

Cheers

like image 301
AntonSack Avatar asked Aug 11 '15 08:08

AntonSack


1 Answers

To avoid jump to its original position add:

animation.fillMode = kCAFillModeForwards
animation.removedOnCompletion = false
like image 115
Hamza Ansari Avatar answered Nov 01 '22 11:11

Hamza Ansari