【问题标题】:Swift: Move UIImage partly along pathSwift:沿路径部分移动 UIImage
【发布时间】:2015-08-11 18:30:06
【问题描述】:

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

不过,还有两个问题:

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

干杯

【问题讨论】:

    标签: swift animation uiimage


    【解决方案1】:

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

    animation.fillMode = kCAFillModeForwards
    animation.removedOnCompletion = false
    

    【讨论】:

      【解决方案2】:

      为避免在动画结束时跳转,在开始动画之前将图像层移动到结束位置:

      imageView.layer.position = CGPoint(x: xEnd, y: yEnd)
      

      部分移动图像恐怕没有简单的方法。另一种方法是计算 endPosition 和基于日落/日出的路径,这样您就不必在动画时间上搞砸了

      【讨论】:

        猜你喜欢
        • 2020-05-12
        • 1970-01-01
        • 2017-10-31
        • 2011-09-03
        • 2011-05-26
        • 2011-08-20
        • 2023-03-25
        • 1970-01-01
        • 2011-10-02
        相关资源
        最近更新 更多