Cody Weaver Cody Weaver - 5 months ago 32
iOS Question

Animate UIView along BezierPath with delay

I want to animate a

UIView
in a figure 8 motion. I am doing this in Swift using
BezierPath
but I want to add a delay to the animation.

let center: CGPoint = CGPoint(x: 60, y: 45)
let cent: CGPoint = CGPoint(x: 90, y: 45)
let radius: CGFloat = 15.0
let start: CGFloat = CGFloat(M_PI)
let end: CGFloat = 0.0
let path1: UIBezierPath = UIBezierPath(arcCenter: center, radius: radius, startAngle: start, endAngle: end, clockwise: true)
let path2: UIBezierPath = UIBezierPath(arcCenter: cent, radius: radius, startAngle: -start, endAngle: end, clockwise: false)
let path3: UIBezierPath = UIBezierPath(arcCenter: cent, radius: radius, startAngle: end, endAngle: -start, clockwise: false)
let path4: UIBezierPath = UIBezierPath(arcCenter: center, radius: radius, startAngle: end, endAngle: start, clockwise: true)
let paths: UIBezierPath = UIBezierPath()
paths.appendPath(path1)
paths.appendPath(path2)
paths.appendPath(path3)
paths.appendPath(path4)

let anim: CAKeyframeAnimation = CAKeyframeAnimation(keyPath: "position")
anim.path = paths.CGPath
anim.repeatCount = 2.0
anim.duration = 3.0

view.layer.addAnimation(anim, forKey: "animate position along path")


The figure 8 works just fine, but I have no idea how to add a delay. I have tried using methods like
animateWithDuration(delay:options:animation:completion:)
but that didn't help. If I am way off base and there is an easier way to animate a
UIView
in a 'Figure 8'/'Inifinity Loop' motion that would be fine. I just need the motion coupled with the delay.

Answer

If you want the animation to trigger after a delay you can try triggering the animation in a function like this:

func executeWithDelay(_ delay: NSTimeInterval, block: dispatch_block_t) {
    let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
    dispatch_after(delay, dispatch_get_main_queue(), block)
}

and use it like:

executeWithDelay(1.0) {
   //trigger animation
}

The above example will trigger the block after 1.0 second.

Comments