[英]How would you reimplement UIControl? How does it fit into the responder chain?
If I were rewriting UIKit, how would I implement UIControl? 如果我重写UIKit,我将如何实现UIControl?
I've been having some trouble fitting UIControl into my conceptual understanding of touch handling & the responder chain. 我在使用UIControl进入触摸处理和响应链的概念性理解方面遇到了一些麻烦。 Specifically, I'm confused about who is responsible for calling UIControl's begin
, continue
and endTrackingWithTouch:withEvent
calls. 具体来说,我很担心谁负责调用UIControl的begin
, continue
和endTrackingWithTouch:withEvent
调用。
My naive interpretation would be that UIControl is responsible for handling touch events like any other subclass of UIResponder, in touchesBegan:
and associated methods. 我天真的解释是,UIControl负责处理触摸事件,如touchesBegan:
任何其他子类UIR touchesBegan:
以及相关方法。 This would get complicated, however, since a touch event remains associated with the view that first handles it, and this would create a situation where a touch that originated on a button but continued as a pan across the screen would continue being handled by that button, which seems counter-intuitive. 然而,这将变得复杂,因为触摸事件仍然与首先处理它的视图相关联,并且这将产生这样的情况:源自按钮但在屏幕上作为平移继续的触摸将继续由该按钮处理,这似乎违反直觉。 I could imagine that the button could begin forwarding those touch events to its superview in this case, but that seems ugly. 在这种情况下,我可以想象按钮可以开始将这些触摸事件转发到其超级视图,但这看起来很难看。 This also all gets slightly confusing because of the UIControlEvents, such as touchUpInside, which suggest that touches are being processed elsewhere and the UIControl is only being notified of discreet events. 由于UIControlEvents(例如touchUpInside)表明正在其他地方处理触摸并且UIControl仅被通知了谨慎的事件,因此这一切也有点令人困惑。
My best guess, here, is that UIControls are handled differently when the view-heirarchy is being walked to find a responder, and that the topmost non-control responder checks of any of its subviews are UIControls, and then calls the correct methods as required, but this also seems a little strange. 我最好的猜测是,在查看视图-heirarchy以找到响应者时,UIControls的处理方式不同,并且最顶层的非控制响应者检查其任何子视图都是UIControls,然后根据需要调用正确的方法,但这似乎有点奇怪。
Does anybody have any guidance or clarification on this point? 有没有人对这一点有任何指导或澄清? I've dug through the documentation a bit but haven't found anything explicit. 我稍微挖了一下文档,但没有发现任何明确的内容。
UIControl
is a subclass of UIView, so it can detects users interactions and it uses the target-action pattern to invoke some methods/actions on objects/targets. UIControl
是UIView的子类,因此它可以检测用户交互,并使用目标操作模式来调用对象/目标上的某些方法/操作。
As you said, in Apple way : "An Event Travels Along a Specific Path Looking for an Object to Handle It" 正如你所说的那样,以Apple的方式说: “一个事件沿着特定的路径前进, 寻找一个可以处理它的对象”
So the basic workflow for UIControl
: 所以UIControl
的基本工作流程:
UIControl
detects basic events ( UIControlEventTouchDown
, UIControlEventTouchCancel
...), the event is handled by the chain Responder workflow. UIControl
检测基本事件( UIControlEventTouchDown
, UIControlEventTouchCancel
...),该事件由链响应者工作流程处理。
addTarget:action:forControlEvents:
tells a UIControl
to invoke the action on the target for the event type argument. addTarget:action:forControlEvents:
告诉UIControl
在目标上调用事件类型参数的操作。
When the eventType and the target will match, the action will be fire. 当eventType和目标匹配时,操作将被触发。
EDIT 编辑
UIControl
is just an interface to provide target/action pattern behavior to the objects that implements it. UIControl
只是一个为实现它的对象提供目标/操作模式行为的接口。
So what's really happen is that when you touch the screen, there is a basic UIEvent that is created with a timestamp, a type (Touches, Motion or RemoteControl), and a subtype. 所以真正发生的是当你触摸屏幕时,有一个基本的UIEvent,它是用时间戳,类型(触摸,运动或远程控制)和子类型创建的。
iOS will collect into this event all UITouch
related to this event ( one UITouch
= one finger touch action) . iOS将收集与此事件相关的所有UITouch
(一个UITouch
=一个手指触摸动作)。 A UITouch contains the UIView
and the UIWindow
where the touch occur and other stuffs. UITouch包含触摸发生的UIView
和UIWindow
以及其他内容。
There are three functions in UIEvent : UIEvent有三个功能:
allTouches()
: That returns a NSSet
of UITouch
allTouches()
:返回UITouch
的NSSet
touchesForView(aView)
: That returns a NSSet
of UITouch
for a specific UIView
touchesForView(aView)
:为特定的UIView
返回一个UITouch
的NSSet
touchesForWindow(aWindow)
: That returns a NSSet
of UITouch
for a specific UIWindow
touchesForWindow(aWindow)
:返回特定UIWindow
的UITouch
的NSSet
From my understanding, when the Event is fired, the system will send it to your UIApplication
, and it will send the event to the FirstResponderChain workflow. 根据我的理解,当事件被触发时,系统会将其发送到您的UIApplication
,它会将事件发送到FirstResponderChain工作流程。
So, I think that when is going down in the View Hierarchy, it checks if the touchesForView
method returns something, if it's return something then it call the appropriate method on the UIView
that implements UIResponder
), so it can call touchesBegan:withEvent:
for example. 因此,我认为当在View Hierarchy中进行下去时,它会检查touchesForView
方法是否返回一些东西,如果它返回了一些东西,然后它调用实现UIResponder
的UIView
上的相应方法,那么它可以调用touchesBegan:withEvent:
for例。 Else it keeps going. 此外,它继续前进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.