美文网首页
beginTrackingWithTouch:withEvent

beginTrackingWithTouch:withEvent

作者: 阿弥妥佛 | 来源:发表于2017-08-26 12:04 被阅读0次

    基于Stack Overflow上的回答,做一个小的整理。大概总结下来就是touchesBegan属于UIResponder,适用于需要更多的自定义控制,但是同时也要自己管理events状态的变更;而beginTracking则属于UIControl,系统方法内建的管理events状态,但是可能没有能力做到更多的自定义控制。
    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.

    相关文章

      网友评论

          本文标题:beginTrackingWithTouch:withEvent

          本文链接:https://www.haomeiwen.com/subject/nlmmdxtx.html