Swift:沿路径部分移动 UIImage

2024-02-08

我正在使用此代码沿曲线移动 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")

尽管如此,仍然存在两个问题:

  • 我想沿着路径仅部分移动图像(根据当前时间与日出/日落) - 如何做到这一点?
  • 动画停止后图像跳转到其原始位置 - 如何避免这种情况?

Cheers


为了避免跳转到其原始位置,请添加:

animation.fillMode = kCAFillModeForwards
animation.removedOnCompletion = false
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Swift:沿路径部分移动 UIImage 的相关文章

随机推荐