beginTrackingWithTouch:withEvent:和touchesBegan:withEvent用法區(qū)別

基于Stack Overflow上的回答,做一個小的整理。大概總結(jié)下來就是touchesBegan屬于UIResponder,適用于需要更多的自定義控制,但是同時也要自己管理events狀態(tài)的變更;而beginTracking則屬于UIControl,系統(tǒng)方法內(nèi)建的管理events狀態(tài),但是可能沒有能力做到更多的自定義控制。
beginTrackingWithTouch:withEvent:

  1. subclass UIControl
  2. Sent to the control when a touch related to the given event enters the control’s bounds.
  3. To provide custom tracking behavior (for example, to change the highlight appearance).To do this, use one or all of the following methods: beginTrackingWithTouch:withEvent:, continueTrackingWithTouch:withEvent:, endTrackingWithTouch:withEvent:
    touchesBegan:withEvent:
  4. subclass UIResponder
  5. Tells the receiver when one or more fingers touch down in a view or window.
  6. There are two general kinds of events: touch events and motion events. The primary event-handling methods for touches are touchesBegan:withEvent:, touchesMoved:withEvent:, touchesEnded:withEvent:, and touchesCancelled:withEvent:. The parameters of these methods associate touches with their events—especially touches that are new or have changed—and thus allow responder objects to track and handle the touches as the delivered events progress through the phases of a multi-touch sequence. Any time a finger touches the screen, is dragged on the screen, or lifts from the screen, a UIEvent object is generated. The event object contains UITouch objects for all fingers on the screen or just lifted from it.

shareimprove this answer

Thanks. To put it into context: I have a custom control which contains a bunch of subviews (e.g. labels). The user should be able to drag those labels around, and they interact with eachother (during motion) in a way that is governed by the main control (container). Which family of touch methods should I be using to implement the dragging: the ones provided by UIControl, or the ones provided by UIResponder? – rmp251 Jul 21 '13 at 18:40

Having just run into this today, I think the key difference is that beginTrackingWithTouch and friends are only for tracking - not anything else - in particular not for target/action handling. So if you override touchesBegan, then you'd also be responsible for calling sendActionsForControlEvents when touches ended. But if you use beginTrackingWithTouch, that's handled for free.
I discovered this by implementing beginTrackingWithTouch (for a custom button control) thinking it was just a sideways replacement for handling touchesBegan. So in endTrackingWithTouch, I called sendActionsForControlEvents if touchInside was true. The end result was that the action was called twice, because first the builtin mechanism sent the action, then I called it. In my case, I'm just interesting in customizing highlighting, so took out the call to sendActionsForControlEvents, and all is good.
Summary: use beginTrackingWithTouch when all you need to do is customize tracking, and use touchesBegan when you need to customize the target/action handling (or other low-level details) as well.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容