我有一个名为 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
的 subview ,container
是添加到RippleButton
的 View
当我在第一次触摸之后触摸它时,动画开始播放,但 IBAction 没有触发。为什么它会触发一次,然后再也不会触发?
最佳答案
你说“波纹是添加到容器的 subview ,容器是添加到 RippleButton 的 View ”......你是在“波纹”效果之后删除那个容器吗?也许这就是获取触摸事件。
关于ios - 自定义 UIButton 触发 IBAction 一次,然后再也不会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43374216/