Ios 取消UIControl animateWithDuration以开始新动画
我很难创建具有以下行为的非常简单的方形按钮:Ios 取消UIControl animateWithDuration以开始新动画,ios,xcode,swift,core-animation,Ios,Xcode,Swift,Core Animation,我很难创建具有以下行为的非常简单的方形按钮: 当我把手指放在按钮上时,它应该缩小一点(动画) 当我用手指按住按钮时,按钮应保持其小状态 当我抬起手指时,按钮应恢复到初始大小(动画) 问题是,当我在播放动画时触摸/抬起时,按钮应停止当前动画并收缩/增长。现在,我只能在完全播放“生长/收缩”动画后与按钮交互 我尝试在触摸功能中使用layer.removeAllimations()函数。。。没有成功。我尝试为视图本身和层设置动画 这里是我尝试的UIControl,我用来建立我的自定义按钮 impo
- 当我把手指放在按钮上时,它应该缩小一点(动画)
- 当我用手指按住按钮时,按钮应保持其小状态
- 当我抬起手指时,按钮应恢复到初始大小(动画)
import UIKit
class TriggerPad: UIControl {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
// I init the Frame with w:200 h:100 in my ViewController
super.init(frame: frame)
self.backgroundColor = UIColor.clearColor()
self.layer.frame = frame
self.layer.backgroundColor = UIColor.blueColor().CGColor
self.multipleTouchEnabled = true
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let oldFrame = self.layer.frame
self.layer.removeAllAnimations()
UIView.animateWithDuration(2) { () -> Void in
self.layer.frame.size.height = 50
self.layer.frame.size.width = 100
// I need to adjust the origin here
}
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
let oldFrame = self.layer.frame
self.layer.removeAllAnimations()
UIView.animateWithDuration(2) { () -> Void in
self.layer.frame.size.height = 100
self.layer.frame.size.width = 200
// I need to adjust the origin here
}
}
}
您可以通过向UIView添加动画选项来解决此问题。AllowUserInteraction。您的代码是:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
UIView.animateWithDuration(2.0, delay: 0.0, options: UIViewAnimationOptions.AllowUserInteraction, animations: { () -> Void in
self.layer.frame.size.height = 50
self.layer.frame.size.width = 100
// I need to adjust the origin here
}, completion: nil)
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
UIView.animateWithDuration(2.0, delay: 0.0, options: UIViewAnimationOptions.AllowUserInteraction, animations: { () -> Void in
self.layer.frame.size.height = 100
self.layer.frame.size.width = 200
// I need to adjust the origin here
}, completion: nil)
}
但我认为你可以得到与你提到的应用程序类似的效果:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
UIView.animateWithDuration(2.0, delay: 0.0, options: UIViewAnimationOptions.AllowUserInteraction, animations: { () -> Void in
self.transform = CGAffineTransformMakeScale(0.6, 0.6)
}, completion: nil)
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
UIView.animateWithDuration(2.0, delay: 0.0, options: UIViewAnimationOptions.AllowUserInteraction, animations: { () -> Void in
self.transform = CGAffineTransformIdentity
}, completion: nil)
}
因为这样可以保持视图的中心。其他信息。它应该像iPhone应用程序Keezy中的垫子一样工作。