Nitesh
Nitesh

Reputation: 2034

Rotating button Clockwise and then back to original position

I have a thumbs up image. When user clicks on it, i'm rotating the image upside down (like thumbs down ). Then when he clicks on the button again, i'm rotating it back to the thumbs up position. But when the image rotates backs to thumbs up position, it moves very fast. I want it to be in duration of what i have given clockwise 180 position.

Dummy code of how i'm doing it

@IBOutlet var likeButton: UIButton!

@IBOutlet var likeCountLabel: UILabel!
var counter = 0
var count = 10

@IBAction func likeButton(sender: UIButton)
{

    let anim = CABasicAnimation(keyPath: "transform.rotation")


    if counter == 0
    {
        anim.fromValue = M_PI
        anim.toValue = 0
        anim.additive = true
        anim.duration = 0.50
        likeButton.layer.addAnimation(anim, forKey: "rotate")
        likeButton.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI))
        count -= 1
        likeCountLabel.text = "\(count)"

        counter = 1

    }
    else if counter == 1
    {
        anim.fromValue = M_PI
        anim.toValue = 0
        anim.additive = true
        anim.duration = 0.50

        likeButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI*2))

        count += 1
        likeCountLabel.text = "\(count)"
        counter = 0

    }

Upvotes: 2

Views: 182

Answers (1)

Apurv
Apurv

Reputation: 17186

In the second part you missed to add animation to like button.

likeButton.layer.addAnimation(anim, forKey: "rotate")

Upvotes: 1

Related Questions