Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Using an image based animations using Swift.

I am trying to implement an image based animation using Swift. I have the image array, duration, and repeat count in the viewDidLoad.

 override func viewDidLoad() {
    super.viewDidLoad()

    var animationImages:[UIImage] = [
        UIImage(named: "dicey-die1")!,
        UIImage(named: "dicey-die2")!,
        UIImage(named: "dicey-die3")!,
        UIImage(named: "dicey-die4")!,
        UIImage(named: "dicey-die5")!,
        UIImage(named: "dicey-die6")!,
        UIImage(named: "dicey-die1")!,
        UIImage(named: "dicey-die2")!,
        UIImage(named: "dicey-die3")!,
        UIImage(named: "dicey-die4")!,
        UIImage(named: "dicey-die5")!,
        UIImage(named: "dicey-die6")!,
        UIImage(named: "dicey-die1")!,
        UIImage(named: "dicey-die2")!,
        UIImage(named: "dicey-die3")!,
        UIImage(named: "dicey-die4")!,
        UIImage(named: "dicey-die5")!,
        UIImage(named: "dicey-die6")!,
        UIImage(named: "dicey-die1")!,
        UIImage(named: "dicey-die2")!,
        UIImage(named: "dicey-die3")!,
        UIImage(named: "dicey-die4")!,
        UIImage(named: "dicey-die5")!,
        UIImage(named: "dicey-die6")!
    ]

    var animationDuration: NSTimeInterval = 1.0
    var animationRepeatCount: Int = 1

}

With the auto complete I am supposed to use The CAAnimation property animationDidStart(), in the parentheses it says to use anim: CAAnimation! How do I use it? I am trying to animate some dice in my motionEnded function.

override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
    dieImage0.image = randomImages.randomDice();
    dieImage1.image = randomImages.randomDice();
    dieImage2.image = randomImages.randomDice();
    dieImage3.image = randomImages.randomDice();
    dieImage4.image = randomImages.randomDice();
    dieImage5.image = randomImages.randomDice();
    dieImage6.image = randomImages.randomDice();
    dieImage7.image = randomImages.randomDice();

    animationDidStart(anim: CAAnimation!)

    println("Motion Ended")
}

I have several 2D images of dice, I want to make an animation of the dice rolling by displaying multiple images in succession. I think I have everything ready, but I don't know how to start the animation when there is a shake gesture

1 Answer

I figured this one out, I needed to use the startAnimating() method, like so:

override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
    dieImage0.image = randomImages.randomDice();
    dieImage1.image = randomImages.randomDice();
    dieImage2.image = randomImages.randomDice();
    dieImage3.image = randomImages.randomDice();
    dieImage4.image = randomImages.randomDice();
    dieImage5.image = randomImages.randomDice();
    dieImage6.image = randomImages.randomDice();
    dieImage7.image = randomImages.randomDice();

    dieImage0.startAnimating()  <===========
    println("Motion Ended")
}