How to find out what view a touch event ended at? How to find out what view a touch event ended at? ios ios

How to find out what view a touch event ended at?


Use hit-testing. You know the location of this point as a CGPoint in terms of the ultimate superview, self.view. Then you can ask self.view which of its subviews that point is in:

CGPoint location = [touch locationInView:self.view];UIView* v = [self.view hitTest:location withEvent:nil];

The UIView v is the view you're looking for.


It is very easy to detect your finally touched view, try this code

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{    CGPoint location = [[touches anyObject] locationInView:self.view];    CGRect fingerRect = CGRectMake(location.x-5, location.y-5, 10, 10);    for(UIView *view in self.view.subviews){        CGRect subviewFrame = view.frame;        if(CGRectIntersectsRect(fingerRect, subviewFrame)){            //we found the finally touched view            NSLog(@"Yeah !, i found it %@",view);        }    }}


In general, you need to take the point where the touch ended, and determine whether it lies within the view you're interested in. I do this using code similar to the following:

CGPoint newCenter = dragView.center;for (UIView *v in dropTargets){    CGRect convertedTargetFrame = [v.superview convertRect:v.frame toView:nil];    if (CGRectContainsPoint(convertedTargetFrame, newCenter))    {        activeTargetIndex = idx;    }}

(this code was edited on the fly to take out a lot of irrelevant stuff, but I keep a list of potential drop targets in dropTargets, and this is just looking at that list to see which of the potential UIViews might contain the point).

The essential thing is that if you know the view or views you're potentially dragging the item to, then you can determine whether that view's frame contains the point using CGRectContainsPoint. The important thing is to keep in mind they may lie in different coordinate systems and it may be necessary to convert from one to the other before your comparison.