我有一个名为RippleButton的自定义UIButton声明如此
public class RippleButton: UIButton
基本上它会在触摸时产生连锁效果。我正在使用此自定义按钮以及以下IBAction
@IBAction func toggleMic() {
if isMicrophoneOn {
print("Stopped recording")
micIcon.image = UIImage(named: "mic_off")
micLabel.text = "Start Recording"
} else {
print("Started recording")
micIcon.image = UIImage(named: "mic_on")
micLabel.text = "End Recording"
}
isMicrophoneOn = !isMicrophoneOn
}
通过Touch Up Inside事件的设计编辑器将操作分配给按钮。
我的RippleButton覆盖了touchesBegan,就像这样
override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
ripple.animate(sender: self, location: touches.first!.location(in: self))
}
当我第一次点击按钮触发IBAction并从我的班级Ripple中播放动画时
func animate(sender: UIView, location: CGPoint){
ripple?.transform = CGAffineTransform(scaleX: 1, y: 1)
let size = Double(sender.bounds.width) > Double(sender.bounds.height) ? sender.bounds.width : sender.bounds.height
ripple?.frame = CGRect(x: location.x - size / 2, y: location.y - size / 2, width: size, height: size)
ripple?.layer.cornerRadius = size / 2.0
ripple?.alpha = CGFloat(opacity)
ripple?.backgroundColor = color
ripple?.transform = CGAffineTransform(scaleX: 0, y: 0)
container?.frame = CGRect(x: 0, y: 0, width: sender.frame.size.width, height: sender.frame.size.height)
container?.layer.cornerRadius = sender.layer.cornerRadius
container?.clipsToBounds = true
UIView.animate(withDuration: TimeInterval(speed),
delay: 0.0,
options: .curveEaseOut,
animations: {
self.ripple?.transform = CGAffineTransform(scaleX: 2.5, y: 2.5)
self.ripple?.alpha = 0.0
})
}
ripple
是添加到container
的子视图,container
是添加到RippleButton
的视图
当我在第一次触摸后触摸动画播放时,但IBAction不会触发。它为什么会发射一次,然后再发射一次?
答案 0 :(得分:1)
你说&#34; ripple是添加到容器的子视图,容器是添加到RippleButton的视图&#34; ...你是否在&#34; ripple&#34;之后删除了该容器?影响?也许那是在接触触摸事件。
答案 1 :(得分:0)
默认情况下,通过调用self.sendActionsForControlEvents触发touchesEnded中的操作。尝试覆盖touchesEnded和touchesCancelled(使用超级调用)并在两者中设置断点。也许这个活动第二次会被取消。在这种情况下,您的动画会以某种方式干扰内部按钮状态实现。这很难解决,因为UIButton是一个黑盒子,更糟糕的是它的内部实现可能会改变。那么你应该从UIControl派生出来。