Moussdog
Moussdog

Reputation: 67

How to stop my UIButton animation SWIFT

I made an animation with UIButton via somes images. Here's the code:

         @IBAction func Touchdown(sender: AnyObject) {
    Izer.setImage(image1, forState: UIControlState.Normal)
    Izer.imageView!.animationImages = [image1, image2, image3, image4,                    image5,
        image6, image7,image8]
    Izer.imageView!.animationDuration = 0.9
    Izer.imageView!.startAnimating()
            playButton.enabled = false



}





@IBAction func TouchUp(sender: AnyObject) {

    soundRecorder.stop()
    playButton.enabled = true


}

When I touch the button , the animation start. But I want to stop it via my Touchup function.

How can I do it ?

Thank you , and sorry for my bad english :(

Upvotes: 0

Views: 2743

Answers (2)

Matt Le Fleur
Matt Le Fleur

Reputation: 2865

Add this to your TouchUp func:

Izer.imageView!.stopAnimating()

p.s. A good place to find information about functions is in the Apple documentation - it really is quite good. So this is the page for an imageView and if you look on the left under tasks, or if you scroll down, you can see the functions and properties you can call and set for animating an imageView.

Upvotes: 2

TheAppMentor
TheAppMentor

Reputation: 1099

Ok, looks like you want the button to act as a toggle switch. On the first touch the button starts animating an imageview and records something. When touched again the animation stops and the button is enabled again.

You can achieve this by declaring a bool variable that tracks the state of the button. If the boolean value is set to true, you run the animation & recording. If the boolean value is false, you stop the and recording.

Here is the sample code :

class ViewController: UIViewController {

@IBOutlet weak var mainView: UIView!
var isButtonPressed = false{
    // Adding a Property Observer, that reacts to changes in button state
    didSet{
        if isButtonPressed{
            // Run the Recording function & Animation Function.
            startAnimationAndRecording()
        }else{
            // Stop the Recoding function & Animation Function.
            stopAnimationAndRecording()
        }
    }
}

@IBAction func changeButtonValue(sender: UIButton) {
    // Toggle the button value.
    isButtonPressed = !isButtonPressed
}

func startAnimationAndRecording(){
    // Add your animation and recording code here.
}

func stopAnimationAndRecording(){
    //Add your stop Animation & Stop Recording code here.
}

}

Upvotes: 1

Related Questions