【问题标题】:Reset animation after removeAllAnimations()removeAllAnimations() 后重置动画
【发布时间】:2016-08-15 09:52:31
【问题描述】:

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

@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()
}

【问题讨论】:

    标签: ios swift xcode uiviewanimation


    【解决方案1】:

    更正了您的 stopButtonAnimation 函数。

    应用变换的作用是将按钮的比例设置为原始值,这样您就可以再次看到动画。

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

    原始代码中的动画在比例值 0.5 处停止,因此下次单击该按钮时,您将看不到它(因为它的动画比例从 0.5 到 0.5)。

    【讨论】:

    • 谢谢。我希望有一个简单的解决方法;)
    • 我很高兴它有帮助:)
    • 关键要点 -> 将任何动画恢复到初始状态!在完全不同的环境中为我工作。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多