icekomo
icekomo

Reputation: 9466

rotate image on button press

I'm trying to rotate and image 180 degrees when a button is pressed. Right now I can get it to rotate the 180, but when I press the button again, the image doesn't rotate back. I feel that I'm missing something simple.

The if statement is working, i just simplified the code and only left in the part that isn't working.

        if(self.dropdownConstraint.constant == -78)
    {
        //roate arrow up
        UIView.animateWithDuration(0.5, animations: {
            self.arrow.transform = CGAffineTransformMakeRotation((180.0 * CGFloat(M_PI)) / 180.0)
            self.view.layoutIfNeeded()
        })
    }
    else
    {
        //roate arrow down
        UIView.animateWithDuration(0.5, animations: {
            self.arrow.transform = CGAffineTransformMakeRotation((-180.0 * CGFloat(M_PI)) / -180.0)
            self.view.layoutIfNeeded()
        })            
    }

Thanks in advance!

Upvotes: 3

Views: 1687

Answers (2)

Peter Willsey
Peter Willsey

Reputation: 1098

Set the transform to identity to undo your previous transform:

UIView.animateWithDuration(0.5, animations: {
    self.arrow.transform = CGAffineTransformIdentity;
    self.view.layoutIfNeeded()
})

Upvotes: 0

pbasdf
pbasdf

Reputation: 21536

The transforms aren't cumulative, so the first rotates by 180 degrees (starting from 0), and the second rotates by -180 degrees (also starting from zero), which appears the same. Just amend the reverse transform to set the rotation back to zero:

    UIView.animateWithDuration(0.5, animations: {
        self.arrow.transform = CGAffineTransformMakeRotation(0)
        self.view.layoutIfNeeded()
    })

Upvotes: 4

Related Questions