【问题标题】:Put object along the path and then move it (IMG INCLUDED)沿路径放置对象,然后移动它(包括 IMG)
【发布时间】:2015-12-15 10:36:28
【问题描述】:

我有一个问题(;)),我需要你的帮助。

让我们看一下图片:

1) 我有一条路。假设它是这样的:

let bezierPath = UIBezierPath()
bezierPath.moveToPoint(CGPointMake(10.5, 47.5))
bezierPath.addCurveToPoint(CGPointMake(45.5, 23.5), controlPoint1: CGPointMake(10.5, 47.5), controlPoint2: CGPointMake(32.5, 23.5))
bezierPath.addCurveToPoint(CGPointMake(84.5, 47.5), controlPoint1: CGPointMake(58.5, 23.5), controlPoint2: CGPointMake(84.5, 47.5))
bezierPath.addLineToPoint(CGPointMake(10.5, 47.5))
bezierPath.closePath()
UIColor.redColor().setStroke()
bezierPath.lineWidth = 1
bezierPath.stroke()

2) 我有一个 UIImageView。第一个问题是:如何将它放在路径的指定部分(A点)的顶部?

3) 第二个问题:如何将其从 A 点动画到 B 点?

【问题讨论】:

  • 你看过关键帧动画吗?
  • 是的,但是这些知识并不能帮助我解决这个问题

标签: ios swift animation path


【解决方案1】:

通过使用CAKeyframeAnimation,您可以使用您制作的路径创建动画

let animation = CAKeyframeAnimation()

// Could also be position.x or position.y if you want to animate a separate axis.
animation.keyPath = "position"
animation.repeatCount = 0 // How many times to repeat the animation
animation.duration = 5.0 // Duration of a single repetition

animation.path = bezierPath.CGPath

然后将其附加到图像的图层

imageView.layer.addAnimation(animation, forKey: "move image along bezier path")

另一个stackoverflow question 帮助我形成了这个答案,如果一切都失败了,你可以随时参考docs

【讨论】:

  • 感谢您的回答,但它不会制作从 A 点到 B 点的动画,而是通过所有 bezierPath
  • 是否将路径本身更改为从 A 开始并在 B 结束不是一个选项?
  • 我的意思是不要使用上述路径,而是更改定义路径的点,以便动画仍然通过整个 bezierPath,但路径本身只会从点 A 到 B
  • 我需要动态更改 B 点,所以这不是我的解决方案。我也需要第一条完整的路径
  • @hannad 你能告诉我如何根据贝塞尔路径更改 UIImageView 图像吗?
猜你喜欢
  • 2011-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多