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

2019-01-14 16:56发布

问题:

I wish to drag a UIImage on to one of several UIButtons and have it repositioned based on which button I drag it to.

The problem I've ran in to is that UITouch only records the view where my touch began, I'd like to access the view my touch ends at. How can I do this?

Code:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];

    dealerBtnOrigin = [touch locationInView:self.view];

    //NSLog(@"%u %u",touch.view.tag, ((UIView *)[dealerBtns objectAtIndex:[table getButtonSeat]]).tag);
    //CHECK TO SEE WHICH BUTTON WAS TOUCHED
    if (touch.view == ((UIView *)[dealerBtns objectAtIndex:[table getButtonSeat]]))
    {
        ((UIView *)[dealerBtns objectAtIndex:[table getButtonSeat]]).center = location;
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];


    if (touch.view == ((UIView *)[dealerBtns objectAtIndex:[table getButtonSeat]]))
    {
        ((UIView *)[dealerBtns objectAtIndex:[table getButtonSeat]]).center = location;
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];
    UIView *endView = [self.view hitTest:location withEvent:nil];

    if (touch.view == ((UIView *)[dealerBtns objectAtIndex:[table getButtonSeat]]))
    {
        NSLog(@"%u %u",endView.tag, touch.view.tag);
        if([buttons containsObject:(UIButton *)endView])
        {
            [[dealerBtns objectAtIndex:[table getButtonSeat]] setHidden:YES];
            [table passButton:touch.view.tag];
            [[dealerBtns objectAtIndex: touch.view.tag] setHidden:NO];
        }
        ((UIView *)[dealerBtns objectAtIndex:[table getButtonSeat]]).center = dealerBtnOrigin;
    }

}

回答1:

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);
        }

    }

}


回答2:

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.



回答3:

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.



回答4:

Assuming there is a @property of the target view in your container, and the target view is added to the container:

@property (nonatomic, strong) UIView* targetView;

// ...

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSSet* touchesForTargetView = [event touchesForView:self.targetView];
    if (touchesForTargetView.allObjects.count != 0) {
        // touch was at target view
    }
    else {
        // touch  was somewhere else
    }    
}


回答5:

swift 4:

override func touchesEnded(_ touches: Set, with event: UIEvent?) {

    if let touch = touches.first{
        let location = touch.location(in: self.view)
        let v = touch.view

// go on.. }

}