基于Stack Overflow上的回答,做一个小的整理。大概总结下来就是touchesBegan属于UIResponder,适用于需要更多的自定义控制,但是同时也要自己管理events状态的变更;而beginTracking则属于UIControl,系统方法内建的管理events状态,但是可能没有能力做到更多的自定义控制。
beginTrackingWithTouch:withEvent:
- subclass UIControl
- Sent to the control when a touch related to the given event enters the control’s bounds.
- 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: - subclass UIResponder
- Tells the receiver when one or more fingers touch down in a view or window.
- 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.
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.