-2

This post describes how to chain repeating UIView animations in Objective-C. However, it requires performSelector, which doesn't appear to be available in Swift.

How can you chain repeating UIView animations in Swift? The goal is to create an animation to shake/wobble a UIView.

Community
  • 1
  • 1
Crashalot
  • 33,605
  • 61
  • 269
  • 439
  • Have you tried animations with `animateWithDuration` and then set the `options` block to `.Repeat`? – Cesare May 06 '15 at 14:48
  • Yes @CeceXX but .Repeat means the other animations never get called because the completion block never gets called. – Crashalot May 06 '15 at 14:49
  • Can't you place your code inside a function and then call the function in the completion block? – Cesare May 06 '15 at 16:22
  • I agree @Crashalot I don't know why the downvotes?? This is a very valid question, and NO the answers below don't answer the question -- if you use .Repeat with an animation--**the completion block never gets called!!** just like the author stated. If anyone knows how to repeat animations that are already Chained then please do answer – FireDragon Sep 06 '15 at 22:36

1 Answers1

0

How about:

var wobble = true

func wobbleLeft() {

    if !wobble {
        return
    }

    UIView.animateWithDuration(0.5, animations: { () -> Void in
        // Animate wobble to left
        }) { (finished) -> Void in
            self.wobbleRight()
    }
}

func wobbleRight() {

    if !wobble {
        return
    }

    UIView.animateWithDuration(0.5, animations: { () -> Void in
        // Animate wobble to right
        }) { (finished) -> Void in
            self.wobbleLeft()
    }
}

Start by calling wobbleLeft(), then it will call wobbleRight() when it's completed. To stop the wobble, just set wobble = false

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160