沿路径放置对象,然后移动它(包含IMG) [英] Put object along the path and then move it (IMG INCLUDED)

查看:93
本文介绍了沿路径放置对象,然后移动它(包含IMG)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题(;)),需要您的帮助.

I have a problem ( ;) ) and I need your help.

让我们看一下图像:

1)我有一条路.可以这样说:

1) I have a path. Lets say it's like this:

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点)的顶部?

2) I have an UIImageView. First question is: How to put it on the top of specified part of the path (A point) ?

3)第二个问题:如何从A点到B点对其进行动画处理?

3) Second question: How to animate it from point A to B ?

推荐答案

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

By using CAKeyframeAnimation, you can create an animation using the path you made

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

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

and then attach it to your image's layer

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

这另一个 stackoverflow问题帮助我形成了这个答案,如果有的话否则,您可以始终参考 docs .

This other stackoverflow question helped me form this answer, and if all else fails, you could always refer to the docs.

这篇关于沿路径放置对象,然后移动它(包含IMG)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆