gpt4 book ai didi

swift - UIButton 心跳动画

转载 作者:IT王子 更新时间:2023-10-29 05:32:43 28 4
gpt4 key购买 nike

我为 UIButton 创建了心跳动画。但是,没有办法停止这个动画,因为它是一个无限循环的代码。在修改了许多 UIView 动画代码块之后,我一直无法获得 UIViewAnimationOptions.Repeat 来生成我需要的内容。如果我能做到这一点,我可以简单地 button.layer.removeAllAnimations() 来删除动画。允许删除动画的编写方法是什么?我在想可能是一个计时器,但如果要同时播放多个动画,那可能会有点困惑。

func heartBeatAnimation(button: UIButton) {

button.userInteractionEnabled = true
button.enabled = true

func animation1() {

UIView.animateWithDuration(0.5, delay: 0.0, options: [], animations: { () -> Void in

button.transform = CGAffineTransformMakeScale(2.0, 2.0)
button.transform = CGAffineTransformIdentity

}, completion: nil)

UIView.animateWithDuration(0.5, delay: 0.5, options: [], animations: { () -> Void in

button.transform = CGAffineTransformMakeScale(2.0, 2.0)
button.transform = CGAffineTransformIdentity

}) { (Bool) -> Void in

delay(2.0, closure: { () -> () in

animation2()

})
}
}

func animation2() {

UIView.animateWithDuration(0.5, delay: 0.0, options: [], animations: { () -> Void in

button.transform = CGAffineTransformMakeScale(2.0, 2.0)
button.transform = CGAffineTransformIdentity

}, completion: nil)

UIView.animateWithDuration(0.5, delay: 0.5, options: [], animations: { () -> Void in

button.transform = CGAffineTransformMakeScale(2.0, 2.0)
button.transform = CGAffineTransformIdentity

}) { (Bool) -> Void in

delay(2.0, closure: { () -> () in

animation1()

})
}
}

animation1()

}

最佳答案

这非常有效。阻尼和 Spring 需要稍微调整一下,但这可以解决问题。 removeAllAnimations() 清除动画并将按钮返回到正常状态。

button.userInteractionEnabled = true
button.enabled = true

let pulse1 = CASpringAnimation(keyPath: "transform.scale")
pulse1.duration = 0.6
pulse1.fromValue = 1.0
pulse1.toValue = 1.12
pulse1.autoreverses = true
pulse1.repeatCount = 1
pulse1.initialVelocity = 0.5
pulse1.damping = 0.8

let animationGroup = CAAnimationGroup()
animationGroup.duration = 2.7
animationGroup.repeatCount = 1000
animationGroup.animations = [pulse1]

button.layer.addAnimation(animationGroup, forKey: "pulse")

这篇文章很有帮助:CAKeyframeAnimation delay before repeating

关于swift - UIButton 心跳动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34729578/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com