ila*_*lan 5 uibutton uiviewanimation ios swift
我需要一个项目中所有按钮的缩放弹簧动画.所以我将UIButton子类化并覆盖触摸事件函数.
import UIKit
class UIAnimatedButton: UIButton {
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.transform = CGAffineTransformMakeScale(0.8, 0.8)
})
super.touchesBegan(touches, withEvent: event)
}
override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
UIView.animateWithDuration(0.5,
delay: 0,
usingSpringWithDamping: 0.2,
initialSpringVelocity: 6.0,
options: UIViewAnimationOptions.AllowUserInteraction,
animations: { () -> Void in
self.transform = CGAffineTransformIdentity
}) { (Bool) -> Void in
super.touchesCancelled(touches, withEvent: event)
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
UIView.animateWithDuration(0.5,
delay: 0,
usingSpringWithDamping: 0.2,
initialSpringVelocity: 6.0,
options: UIViewAnimationOptions.AllowUserInteraction,
animations: { () -> Void in
self.transform = CGAffineTransformIdentity
}) { (Bool) -> Void in
super.touchesEnded(touches, withEvent: event)
}
}
}
Run Code Online (Sandbox Code Playgroud)
这在快速敲击时效果很好但是当我长按(1-2秒)按钮时,我没有在动作事件中进行修饰.当我将它切换回常规UIButton时,一切正常.
任何想法为什么会发生?
而不是在我在那里调用的完成块touchesCancelled和touchesEnded方法中调用super self.sendActionsForControlEvents(UIControlEvents.TouchUpInside).
| 归档时间: |
|
| 查看次数: |
6644 次 |
| 最近记录: |