[英]How do I circulate an image in Swift using/without using UIBezierPath?
I am trying to make an image circulating using UIBezierPath
. 我正在尝试使用
UIBezierPath
使图像循环。 Here is the code I am using to determine the image's x
and y
coordinates, its width and its height. 这是我用来确定图像的
x
和y
坐标,其宽度和高度的代码。
var frm : CGRect = self.image.frame
let originX = frm.origin.x
let originY = frm.origin.y
let originWidth = frm.size.width
let originHeight = frm.size.height
self.image.frame = frm
However, if I put originX
and originY
in this code: 但是,如果我在此代码中放入
originX
和originY
:
let path = UIBezierPath()
path.moveToPoint(CGPoint(x: originX+50, y: originY))
path.addCurveToPoint(CGPoint(x: originX-20, y: originY),
controlPoint1: CGPoint(x: originX+20, y: originY),
controlPoint2: CGPoint(x: originX-10, y: originY))
let anim = CAKeyframeAnimation(keyPath: "position")
// set the animations path to our bezier curve
anim.path = path.CGPath
anim.repeatCount = Float.infinity
anim.duration = 5.0
image.layer.addAnimation(anim, forKey: "animate position along path")
The image just stays at the top left of the screen (instead of being placed close to the center). 图像仅停留在屏幕的左上方(而不是靠近中心放置)。 Why?
为什么? By the way, are there any other ways to make an image circulating other than using
UIPathBezier
? 顺便说一句,除了使用
UIPathBezier
之外,还有其他方法可以使图像循环吗?
Well, first of all you need to override the UIBezierPath()
constructor so you can give parameters as originX
and originY
, also you must use var
instead of let
because var
is a simple variable and let is a constant
and it cannot be modified in Swift. 好吧,首先,您需要重写
UIBezierPath()
构造函数,以便可以将参数指定为originX
和originY
,还必须使用var
而不是let
因为var
是一个简单变量,let是一个constant
,并且不能在Swift中进行修改。
I dont have Xcode with me but the code should look like this: 我没有Xcode,但代码应如下所示:
override func UIBezierPath(#x:CGFloat, andOriginX:CGFloat) {
}
let path = UIBezierPath(x, y)
path.moveToPoint(CGPoint(x: x+50, y: y)) // and so one...
path.addCurveToPoint(CGPoint(x: originX-20, y: originY),
controlPoint1: CGPoint(x: originX+20, y: originY),
controlPoint2: CGPoint(x: originX-10, y: originY))
let anim = CAKeyframeAnimation(keyPath: "position")
// set the animations path to our bezier curve
anim.path = path.CGPath
anim.repeatCount = Float.infinity
anim.duration = 5.0
image.layer.addAnimation(anim, forKey: "animate position along path")
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.