4

I am using animations to transform a button when it is clicked, I was able to make the button bigger. However, I thought that by using .Repeat and .Autoreverse, the button would go back to its normal state. (scale 1.0) but that is not the case! Maybe I misunderstood the tutorials and questions that I read regarding .AnimateWithDuration ??

This is the code that I am using:

 let button = sender as! UIButton

    UIView.animateWithDuration(1.0, delay: 0.6,
        options: [.Repeat, .Autoreverse, .AllowUserInteraction],
        animations:{
        button.transform = CGAffineTransformMakeScale(1.2, 1.2)
        }, completion: nil)

In another question I saw that the problem may be resolved by adding .AllowUserInteraction but that is not the case.

I don't know if it even matters but this code is enclosed within a touch event.

 @IBAction func addButtonClicked(sender: AnyObject) {}

What could be going on here? isn't this how you are supposed to reverse the animation?

Jesus Rodriguez
  • 2,571
  • 2
  • 22
  • 38
  • With the .Repeat and .Autoreverse the button will be "pulsing". The code works as expected here in a test code. @IBAction func pushButton(sender: AnyObject) { let btt = sender as! UIButton UIView.animateWithDuration(1.0, delay: 0.6, options: [.Repeat, .Autoreverse, .AllowUserInteraction], animations:{ btt.transform = CGAffineTransformMakeScale(1.2, 1.2) }, completion: nil) } – Ulysses Jan 31 '16 at 14:25
  • Double check if the button is linked to this function, use a breakpoint to make sure its calling it. – Ulysses Jan 31 '16 at 14:28
  • @UlyssesR it is calling because the button does get bigger when I click it but it does not go back to its normal state – Jesus Rodriguez Jan 31 '16 at 17:22

1 Answers1

5

At the end of the animation you should reset the size of the object.
The .autoreverse just "reverse visually", but does not change the actual object size.

Try this out.

@IBAction func prss(sender: AnyObject) {
    let btt = sender as! UIButton

    UIView.animate(withDuration: 1.0, delay: 0.6, options: [.autoreverse, .allowUserInteraction], animations:{
        btt.transform = CGAffineTransform(scaleX: 5.0, y: 5.0)
    }, completion: { (finished) in
        btt.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
    })
}
Karen Hovhannisyan
  • 1,140
  • 2
  • 21
  • 31
Ulysses
  • 2,281
  • 1
  • 16
  • 24