Search code examples
iphoneuiviewdraguitouchtouchesmoved

detecting the [UITouch view] in touchesMoved method


I wish to drag from one subview to another within my application (and connect them with a line), and so I need to keep track of the current view being touched by the user.

I thought that I could achieve this by simply calling [UITouch view] in the touchesMoved method, but after a quick look at the docs I've found out that [UITouch view] only returns the view in which the initial touch occurred.

Does anyone know how I can detect the view being touched while the drag is in progress?


Solution

  • After a bit more research I found the answer.

    Originally, I was checking for the view like so:

    if([[touch view] isKindOfClass:[MyView* class]])
    {
       //hurray.
    }
    

    But, as explained in my question, the [touch view] returns the view in which the original touch occurred. This can be solved by replacing the code above with the following:

    if([[self hitTest:[touch locationInView:self] withEvent:event] isKindOfClass:[MyView class]])
    {
        //hurrraaay! :)
    }
    

    Hope this helps