I am trying to make an image circulating using UIBezierPath
. Here is the code I am using to determine the image's x
and y
coordinates, its width and its height.
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:
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
?
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.
I dont have Xcode with me but the code should look like this:
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")
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.