iPhone:透明地处理触摸

发布于 2024-09-11 15:19:25 字数 316 浏览 2 评论 0原文

我有一个 UIViewController 和一个与之关联的 UIView。 UIView 有许多子视图。

如何处理 UIViewController 子类中指定矩形内的触摸事件?

这种处理应该是透明的:UIView 及其子视图仍然应该接收其触摸事件,即使它们与指定矩形相交。

PS

  1. 覆盖触摸在视图中开始 控制器不工作。内心观点 不传递事件。
  2. 添加自定义 按钮到 UIView 子视图的末尾 列表也不起作用。内 子视图没有收到触摸 事件。

I have a UIViewController and an associated with it UIView. The UIView has a number of subviews.

How to handle touch event inside a specified rectangle in my UIViewController subclass?

This handling should be transparent: UIView and its subviews should still receive their touch events even if they intersect with the specified rectangle.

P.S.

  1. Overriding touchesBegan in the view
    controller doesn't work. Inner views
    doesn't pass events through.
  2. Adding a custom
    button to the end of UIView subviews
    list also doesn't work. Inner
    subviews doesn't receive touch
    events.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

小忆控 2024-09-18 15:19:25

TouchesBegan/touchesMoved/touchesEnded 可能是要走的路。根据您想要执行的操作,UIGestureRecognizer 也可能是一个选择。

要确保子视图向上传递事件,请将子视图上的 userInteractionEnabled 设置为 YES。

UIView 默认情况下将其设置为 YES,但某些子类(特别是 UILabel)会覆盖此设置并将其设置为 NO。

touchesBegan/touchesMoved/touchesEnded is probably the way to go. Depending on exactly what you are trying to do, UIGestureRecognizer may also be an option.

To make sure subviews pass events up, set userInteractionEnabled to YES on the subviews.

UIView sets this to YES by default, but some subclasses (notably UILabel) override this and set it to NO.

疑心病 2024-09-18 15:19:25

仅仅因为视图默认情况下不传递事件并不一定意味着它必须如此。

您可以对正在使用的视图进行子类化,并让它们将事件传递到视图控制器上。

另一个想法是在所有视图之上有一个透明的处理程序。也就是说,有一个透明的 UIView 位于视图上方,处理触摸事件并传递它们。我不知道这在实践中是否有效,但听起来确实如此。

Just because the views don't pass events through by default doesn't necessarily mean that's the way it has to be.

You could subclass the views that you are using and have them pass the events onto the view controller.

Another idea is to literally have a transparent handler on top of all of your views. That is, have a transparent UIView that sits above your views, handling touch events but also passing them through. I have no idea if this works in practice, but it sounds like it would.

茶花眉 2024-09-18 15:19:25

您的视图及其控制器可以使用touchesBegan、touchesEnded 或touchesMoved 来处理触摸。以 TouchsBegan 为例,您可以选择将事件传递给
响应者链中的下一个响应者。这样,您的每个视图都有机会根据触摸事件执行某些操作。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //do something here and then pass the event on
    [self.nextResponder touchesBegan:touches withEvent:event]; 
}

Your views, and their controllers can handle a touch using touchesBegan, touchesEnded or touchesMoved. Within touchesBegan as an example you can then choose to pass the event to
the next responder in the responder chain. This way each of your views will get a chance to do something based on the touch event.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //do something here and then pass the event on
    [self.nextResponder touchesBegan:touches withEvent:event]; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文