管理自定义 UIControl 上的事件
我将 UIControl 子类化以组成包含不同标准控件的自定义控件。
对于此讨论,我们假设我的自定义 UIControl 仅包含 UIButton。
我想要实现的是,单击自定义 UIControl 中的任意位置都会为该自定义 UIControl 生成单击事件。标准行为是 UIButton 将处理并消耗(即不转发)单击事件。
由于不鼓励子类化 UIButton,所以我真的找不到实现此目的的直接方法。
有什么建议吗?
I am subclassing UIControl to compose a custom control that contains different standard controls.
For this discussion let's assume that my custom UIControl contains a UIButton only.
What I would like to achieve is that clicking anywhere in the custom UIControl generates a click event for that custom UIControl. The standard behavior is that the UIButton will process and consume (i.e. not forward) the click event.
As subclassing UIButton is discouraged, I can't really find a straightforward way of achieving this.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想出了一个简单的解决方案,不需要 UIButton 的子类化。
在为 UIButton 的 TouchUpInside 控件事件定义的操作方法中,我添加了以下代码行:
这会导致在单击自定义 UIControl 中的任意位置时调用 TouchUpInside 控件事件。
I came up with a simple solution that doesn't need subclassing of the UIButton.
In the action method defined for the UIButton's TouchUpInside control event, I have added the following line of code:
This results in the TouchUpInside control event being called, when clicking anywhere in the custom UIControl.
UIView
有一个名为-hitTest:withEvent:
的方法,事件系统使用该方法抓取视图层次结构并将事件分派给子视图。如果您希望父视图吞噬所有可能分派到其子视图的事件,只需使用如下所示的内容覆盖父视图的-hitTest:withEvent:
即可:UIView
s have a method called-hitTest:withEvent:
that the event system uses to crawl the view hierarchy and dispatch events to subviews. If you want a parent view to gobble up all events that might otherwise be dispatched to its subviews, just override the parent's-hitTest:withEvent:
with something like the following:UIButton 旨在处理触摸事件。您可以将
userInteractionEnabled
设置为 NO,让按钮不接受任何触摸,也可以在按钮上使用addTarget:action:forControlEvents:
让它调用方法当按钮被触摸时你的班级。顺便说一句,哪里不鼓励子类化 UIButton ?
UIButton is designed to process touch events. You can either set
userInteractionEnabled
to NO to have the button not accept any touches, or you can useaddTarget:action:forControlEvents:
on the button to have it call a method on your class when the button is touched.BTW, where is subclassing UIButton discouraged?
我经常在
UIResponder 类
中找到有用的用户交互相关任务,它是UIControl - UIButton
的超类。了解
–touchesBegan:withEvent:
、–touchesMoved:withEvent:
、–touchesEnded:withEvent:
、–touchesCancelled:withEvent:< /code> 在 http://developer. apple.com/library/ios/#documentation/UIKit/Reference/UIResponder_Class/Reference/Reference.html 您可能会找到为
UIButton
自定义用户交互的方法。顺便说一句,我认为子类化UIButton
不会有任何问题,无论您听说过什么,只要您的实现正确添加到超类实现中,或者负责任地重写完全是这样。Often I find useful user interaction related tasks in
UIResponder class
, which is the super class ofUIControl - UIButton
.Read about
– touchesBegan:withEvent:
,– touchesMoved:withEvent:
,– touchesEnded:withEvent:
,– touchesCancelled:withEvent:
in http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIResponder_Class/Reference/Reference.html You may find ways to customize user interaction for yourUIButton
. By the way, I don't think there will be any problem subclassingUIButton
, no matter what you've heard, as long as your implementation is correctly added to the super class implementation, or does responsibly override it altogether.