Ant*_*ack 6 animation uiimage swift
我正在使用此代码沿着曲线移动UIImage:
// 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")
Run Code Online (Sandbox Code Playgroud)
尽管如此,还有两个问题:
干杯
为了避免跳到原来的位置,请添加:
animation.fillMode = kCAFillModeForwards
animation.removedOnCompletion = false
Run Code Online (Sandbox Code Playgroud)