ios - 自定义 UIButton 触发 IBAction 一次,然后再也不会

标签 ios swift swift3 uibutton

我有一个名为 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/

相关文章:

ios - 这是处理 Realm 中两个相似对象的正确方法吗?

json - 如何从 firebase 中的键值对中获取唯一的键?

ios - 以编程方式设置约束的问题 Swift

ios - 位置管理器未在 swift ios10 中更新

swift - 当用户已经通过身份验证时,它会在转到 MainController 之前显示 LoginViewController 大约半秒钟

ios - 单击菜单加载 subview

ios - 如何修改 MVVM Cross 中的绑定(bind)值

ios - 我的应用程序使用音频单元在IOS中播放mp3文件,但是当屏幕关闭时,声音停止了

ios - Swift 中的 URL 验证

swift - 在不同的 swift 版本中找不到 sizeForItemAtIndexPath