UIScrollView如何从它的子视图窃取? [英] How does UIScrollView steal touches from its subviews?

查看:132
本文介绍了UIScrollView如何从它的子视图窃取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要能够创建自己的UIView的容器子类,它们可以在子视图之前首先做出反应。这是棘手的,因为通常子视图在超看之前接收触摸事件(通过 touchesBegan:等)。 UIScrollView如何反转这个?



要清楚,我不是问我UIScrollView 的行为。我明白它的作用,以及你将如何正常使用它。我问的是如何干净地实现我自己的版本 - 不是因为我想,而是因为我试图构建可重用的容器视图,利用类似的行为。


<您可以在 UIView 中实现 hitTest:withEvent: $ c>子类。



此方法被调用以检查子视图必须接收触摸事件,因此您可以在实际发生之前执行一些操作。您也可以更改必须接收活动的子视图。


I'd like to be able to create my own container subclasses of UIView which can react first to touches, before their subviews. This is tricky because normally a subview receives touch events (via touchesBegan: etc) before superviews. How does UIScrollView reverse this?

To be clear, I am not asking how UIScrollView behaves. I understand what it does, and how you would normally use it. I'm asking about how I could cleanly implement my own version of this -- not because I want to, but because I'm trying to build reusable container views that take advantage of similar behavior.

解决方案

You can implement hitTest:withEvent: method in your UIView subclass.

This method gets called to check what subview must receive the touch event, so you can perform some action there before it actually happens. You can also change subview that must receive event.

这篇关于UIScrollView如何从它的子视图窃取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆