简体   繁体   English

UIButton子类忽略“在内部触摸”事件

[英]UIButton Subclass Ignoring 'Touch Up Inside' Event

I have a subclass of a UIButton: 我有一个UIButton的子类:

    import UIKit
    class CustomButton: UIButton {

        required init?(coder decoder: NSCoder) {
            super.init(coder: decoder)

            //Customizing the button appearance
        }
    }

In interface builder, in storyboards, under my main view controller, I dragged and dropped a stock button, and changed it's 'Custom Class' to CustomButton from the drop down menu. 在界面构建器的故事板中,在主视图控制器下,我拖放了一个股票按钮,然后从下拉菜单中将其“自定义类”更改为CustomButton。

Wired it's 'Touch Up Inside' event to an IBAction. 将其“ Touch Up Inside”事件连接到IBAction。

Custom code for appearance shows up, but the button is not firing the 'Touch Up Inside' . 显示了外观的自定义代码,但按钮未触发“ Touch Up Inside”

When I checked for touch events (touch began, ended, eat) within the subclass, the button seems to be receiving touch. 当我在子类中检查触摸事件(触摸开始,结束,吃东西)时,该按钮似乎正在接收触摸。

Try disabling user interaction on the subviews that are part of your UIButton. 尝试在属于UIButton的子视图上禁用用户交互。 This will allow the gestures to continue through to the button itself. 这将使手势继续进行到按钮本身。

userInteractionEnabled = false

Found the issue: 发现了问题:

When I customize the button, I added a UIVisualEffectView as its subview that covered the whole frame, and that canceled the any events the button was able to take. 当我自定义按钮时,我添加了一个UIVisualEffectView作为它的子视图,该子视图覆盖了整个框架,并且取消了按钮能够执行的所有事件。 Quite unfortunate. 相当不幸。

I had to go with a UIView subclass and add a tap gesture to mimic the touch events. 我必须使用UIView子类,并添加点击手势来模仿触摸事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM