Joe Corcoran
Joe Corcoran

Reputation: 23

Starting and Stopping Animation with Button in Swift

I am trying to use a UIButton to start and stop an animated sequence of images that I have placed into an imageview. I am using swift 3.0

Here is my current code:

import UIKit

class ViewController: UIViewController {
    var selectedProgression : Int = 0

    var loading_1: UIImage!
    var loading_2: UIImage!
    var loading_3: UIImage!

    var animatedImage: UIImage!

    @IBOutlet weak var progressionAnimation: UIImageView!

    override func viewDidLoad() {

        loading_1 = UIImage(named: "blues-1")
        loading_2 = UIImage(named: "blues-2")
        loading_3 = UIImage(named: "blues-3")

        var images: [UIImage]!

        images = [loading_1, loading_2, loading_3]

        animatedImage = UIImage.animatedImage(with: images, duration: 3.0)

        progressionAnimation.image = animatedImage

        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

I've looked and have somewhat of an idea of how to do this, but keep getting stuck while actually implementing the solution into my code :(

From what I've found, I need to create an outlet for the button that measures if it is pressed or not pressed, and connect the button action to startAnimating or stopAnimating when pressed, depending on it's current state.

I found the startAnimating and stopAnimating functions on Apple documentation https://developer.apple.com/documentation/uikit/uiimageview/1621061-startanimating but am unsure of how to actually implement them into the code.

Any help would be GREATLY appreciated. Thank you!

Upvotes: 2

Views: 1674

Answers (2)

E.Coms
E.Coms

Reputation: 11531

If you want to use start or stop animation, please not to use images of progreesionAnimation.

    progressionAnimation.animationImages =  images
    progressionAnimation.animationDuration = 1.0
    progressionAnimation.animationRepeatCount = 100
    progressionAnimation.startAnimating()

You are supposed to use the animationImages from the image array you create.

Upvotes: 1

Houwert
Houwert

Reputation: 200

How to create an outlet can be found here. To summarize, open the assistant editor (the two cirles in the top right corner), and drag the button from your storyboard to your Swift file while holding control.

The IBAction you would have to write for this outlet would look something like this:

@IBAction func starStopButtonPressed(_ sender: Any) {
    //Check if the progressionAnimation UIImageView is already animating
    if progressionAnimation.isAnimating { 
        progressionAnimation.stopAnimating() //If animating: stop animating
    else {
        progressionAnimation.startAnimating() //If not animating: start animating
    }
}

When creating new variables be careful with !. This can cause crashes if you try to use the variable before it has a value.

So this:

var images: [UIImage]!

images = [loading_1, loading_2, loading_3]

Should be written like this:

var image: [UIImage] = [loading_1, loading_2, loading_3]

Fewer lines which makes it look cleaner, and it is safer. Do the same for those loading_1-3 UIImages, you can move their declaration to inside the viewDidLoad function.

Just like this:

var animatedImage: UIImage!

animatedImage = UIImage.animatedImage(with: images, duration: 3.0)

progressionAnimation.image = animatedImage

It would be both cleaner, and safer, if you write it like this:

progressionAnimation.image = UIImage.animatedImage(with: images, duration: 3.0)

Upvotes: 1

Related Questions