2016-08-15 45 views
3

在社区的大力帮助下,我制作了放大和缩小按钮图像的动画。问题是,在第一次运行之后,该按钮被禁用,并且在应用重新启动之前不会再工作。我想要的是,单击按钮并且动画完成后,它会返回到起点,然后您可以再次单击它。有人可以帮助我吗?removeAllAnimations()后重置动画

@IBAction func coffeeButton(sender: UIButton) { 
    var timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "stopButtonAnimation", userInfo: nil, repeats: false) 

    let options = UIViewAnimationOptions.Autoreverse | UIViewAnimationOptions.Repeat | UIViewAnimationOptions.CurveEaseInOut 

    UIView.animateWithDuration(0.5, delay: 0, options: options, animations: { 
     self.button.transform = CGAffineTransformMakeScale(0.5, 0.5) 
    }, completion: nil) 
} 

func stopButtonAnimation(){ 
    button.layer.removeAllAnimations() 
} 

回答

5

更正了stopButtonAnimation函数。

应用转换的功能是将按钮的比例设置为原始值,以便您能够再次看到动画。在原始代码

func stopButtonAnimation(){ 
    button.layer.removeAllAnimations() 
    button.layer.transform = CATransform3DIdentity 
} 

动画在0.5,下一时间刻度值停止您按一下按钮,你只是看不到它(因为它已被动画的规模从0.5到0.5)。

+0

谢谢。我希望有一个简单的修复;) –

+0

我很高兴它帮助:) –

+0

关键外卖 - >恢复任何动画到初始状态!在完全不同的环境中为我工作。谢谢。 – inokey