我正在尝试为视图中的按钮设置弹跳效果。我希望我的按钮可以做这样的事情
How to create a UIView bounce animation?
我正在使用此代码执行此操作,但它不流畅......
UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: [], animations: ({
self.image.center.y = self.view.frame.height / 4
}), completion: nil)
UIView.animateWithDuration(0.7, delay: 0.2, usingSpringWithDamping: 1, initialSpringVelocity: 0.8, options: [], animations: ({
self.image.center.y = self.view.frame.height / 6
}), completion: nil)
UIView.animateWithDuration(0.9, delay: 0.4, usingSpringWithDamping: 1, initialSpringVelocity: 0.6, options: [], animations: ({
self.image.center.y = self.view.frame.height / 4
}), completion: nil)
UIView.animateWithDuration(1, delay: 0.6, usingSpringWithDamping: 1, initialSpringVelocity: 0.4, options: [], animations: ({
self.image.center.y = self.view.frame.height / 5.5
}), completion: nil)
UIView.animateWithDuration(1.05, delay: 0.8, usingSpringWithDamping: 1, initialSpringVelocity: 0.2, options: [], animations: ({
self.image.center.y = self.view.frame.height / 4
}), completion: nil)
答案 0 :(得分:0)
不是将所有动画嵌套在第一个动画块中,而是将每个连续动画放在前一个动画块的完成处理程序中。
虽然我建议使用Core Animation这样的东西而不是UIView animateWithDuration方法。
答案 1 :(得分:0)
您可以使用下面的类方便使用
class BounceButton: UIButton {
@IBInspectable var baseColor: String = "2"{
didSet {
//layer.borderColor = borderColor.CGColor
if baseColor == "1" {
self.backgroundColor = UIColor.MiTheme.BtnColorPrimary
self.setTitleColor(UIColor.white, for: .normal)
self.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
}
else if baseColor == "2" {
self.backgroundColor = UIColor.MiTheme.BtnColorSecond
self.setTitleColor(UIColor.black, for: .normal)
}
else if baseColor == "3" {
self.backgroundColor = UIColor.black
self.setTitleColor(UIColor.white, for: .normal)
self.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
}
else if baseColor == "4" {
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
//self.layer.cornerRadius = 28.5
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
//self.layer.cornerRadius = 28.5
}
// Add some animations on button click
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
// Scale up the button
self.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 6, options: .allowUserInteraction, animations: {
// Reset the sizes to defaults
self.transform = CGAffineTransform.identity
}, completion: nil)
}
}