管理自定义 UIControl 上的事件

发布于 2024-10-20 11:08:45 字数 247 浏览 2 评论 0原文

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

墨离汐 2024-10-27 11:08:45

我想出了一个简单的解决方案,不需要 UIButton 的子类化。

在为 UIButton 的 TouchUpInside 控件事件定义的操作方法中,我添加了以下代码行:

[self sendActionsForControlEvents:UIControlEventTouchUpInside];

这会导致在单击自定义 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:

[self sendActionsForControlEvents:UIControlEventTouchUpInside];

This results in the TouchUpInside control event being called, when clicking anywhere in the custom UIControl.

长亭外,古道边 2024-10-27 11:08:45

UIView 有一个名为 -hitTest:withEvent: 的方法,事件系统使用该方法抓取视图层次结构并将事件分派给子视图。如果您希望父视图吞噬所有可能分派到其子视图的事件,只需使用如下所示的内容覆盖父视图的 -hitTest:withEvent: 即可:

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
  if(CGRectContainsPoint([self bounds], point)){
    return self;
  }
  return [super hitTest:point withEvent:event];
}

UIViews 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:

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
  if(CGRectContainsPoint([self bounds], point)){
    return self;
  }
  return [super hitTest:point withEvent:event];
}
九歌凝 2024-10-27 11:08:45

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 use addTarget: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?

琉璃梦幻 2024-10-27 11:08:45

我经常在 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 of UIControl - 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 your UIButton. By the way, I don't think there will be any problem subclassing UIButton, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文