swift UIButton示例中的核心动画
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift UIButton示例中的核心动画相关的知识,希望对你有一定的参考价值。
// Swift 3
extension UIButton {
func pulsate() {
let pulse = CASpringAnimation(keyPath: "transform.scale")
pulse.duration = 0.6
pulse.fromValue = 0.95
pulse.toValue = 1.0
pulse.autoreverses = true
pulse.repeatCount = 2
pulse.initialVelocity = 0.5
pulse.damping = 1.0
layer.add(pulse, forKey: "pulse")
}
func flash() {
let flash = CABasicAnimation(keyPath: "opacity")
flash.duration = 0.5
flash.fromValue = 1
flash.toValue = 0.1
flash.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
flash.autoreverses = true
flash.repeatCount = 3
layer.add(flash, forKey: nil)
}
func shake() {
let shake = CABasicAnimation(keyPath: "position")
shake.duration = 0.1
shake.repeatCount = 2
shake.autoreverses = true
let fromPoint = CGPoint(x: center.x - 5, y: center.y)
let fromValue = NSValue(cgPoint: fromPoint)
let toPoint = CGPoint(x: center.x + 5, y: center.y)
let toValue = NSValue(cgPoint: toPoint)
shake.fromValue = fromValue
shake.toValue = toValue
layer.add(shake, forKey: "position")
}
}
// Example of using the extension on button press
@IBAction func pulseButtonPressed(_ sender: UIButton) {
sender.pulsate()
}
@IBAction func flashButtonPressed(_ sender: UIButton) {
sender.flash()
}
@IBAction func shakeButtonPressed(_ sender: UIButton) {
sender.shake()
}
以上是关于swift UIButton示例中的核心动画的主要内容,如果未能解决你的问题,请参考以下文章
UIButton 动画忽略 Swift 中的目标选择器
使用 Swift 3 为 iOS 中的 UIButton 设置“背景颜色”动画
使用 Swift 自动布局动画 UIButton
在 Swift 中为 UIButton 设置动画
使用swift更改动画块内的UIbutton
动画UIButton回到原来的位置swift IOS