顺时针旋转按钮然后返回原始位置

时间:2016-08-05 05:54:41

标签: ios swift

我有一个竖起大拇指的形象。当用户点击它时,我将图像上下颠倒(如拇指向下)。然后,当他再次点击按钮时,我将其旋转回到竖起位置。但是当图像旋转回到竖起位置时,它会移动得非常快。我想让它在我顺时针180位置的持续时间内。

我如何做的虚拟代码

@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

    }

1 个答案:

答案 0 :(得分:1)

在第二部分中,您错过了将动画添加到按钮中。

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