iosswiftuiviewanimationanimatewithduration

.Repeat .AutoReverse not working


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?


Solution

  • 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)
        })
    }