Trying to change button for start/stop with if/else statement

Trying to get playButton to be able to start and stop an animation by using an if/else statement. The button text will change to Stop but will not stop the animation. Not knowing for sure I’ve tried changing the State Config in the Attributes inspector, with no luck. Im sure its super easy, any help would be awesome. Thank you Herm

import UIKit
import AVFoundation

class ViewController: UIViewController {

@IBOutlet weak var arnoldImageView: UIImageView!
@IBOutlet weak var playButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction func playButtonPressed(_ sender: Any) {
    
    arnoldImageView.animationImages = [UIImage(named: "th1")!,
                                       UIImage(named: "th-2")!,
                                       UIImage(named: "th-3")!,
                                       UIImage(named: "th-4")!]
    
    arnoldImageView.animationDuration = 2.0
    

    if arnoldImageView.isAnimating
        
        {
            //stop button is pressed
            arnoldImageView.stopAnimating()
            playButton.setTitle("START", for: UIControlState.normal)
            playButton.tintColor = UIColor.green
        
        }else{
        
            //start button is pressed
            arnoldImageView.startAnimating()
            playButton.setTitle("STOP", for: UIControlState.normal)
            playButton.tintColor = UIColor.red
    }
}

}

Hi @herm57,
Just a random stab at your problem, have you tried to look at setting the condition based on the button title instead or use a state that set’s to True/False as the button title changes?

There could be something that might still trigger your animation and not stop it, but the button titles change based on the button press event.

cheers,

Jayant

Hi Jayant, this might sound crazy but that’s not a post that I generated! I did generate a question about disabling buttons after a certain condition was met, but not this one! I really don’t know what to say.