19

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

}
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
Declan McKenna
  • 4,321
  • 6
  • 54
  • 72
  • I don't think I can go with a manual solution using CGPoint co-ordinates to find out which button is which as I'm using multiple storyboards for different screen sizes/devices. – Declan McKenna May 02 '13 at 01:18

5 Answers5

23

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I've thrown this in to my code (within touchesEnded) and I'm still getting the view the touch began at. I've updated my the question to contain a hittest. – Declan McKenna May 02 '13 at 18:20
  • After a bit of experimenting it looks like the view being returned is actually the UIImage I'm dragging rather than the original UIButton (they have the same tag) or the button I drag the image to as intended. – Declan McKenna May 02 '13 at 18:32
  • 1
    Yep, I was afraid that might be what would happen. Okay, then the easy solution is to *override* `hitTest:withEvent:` in `self.view` such that when you're performing this *particular* hit-test it doesn't return the view you're dragging (you'll have to tell it which that one is). It will then continue hit-testing and return the view behind this one instead. – matt May 02 '13 at 19:29
  • This was to great help for fixing some views broken in iOS 9+ – Gillsoft AB Oct 28 '15 at 23:33
18

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

    }

}
Thilina Chamath Hewagama
  • 9,039
  • 3
  • 32
  • 45
  • This returned three views: *My apps background image *The UIButton I dragged by small UIImage to (the view I want) *My small UIImage Interesting to see how this works and probably explains why the answer above didn't work for me. Thanks. – Declan McKenna May 02 '13 at 18:44
  • Very very very nice solution, hope the best for you :) – TMMDev Jul 23 '15 at 13:46
  • 1
    You can use CGRectContainsPoint instead of CGRectIntersectsRect for a more accurate intersect. – Johnny Rockex Mar 23 '16 at 14:19
  • Is there any obvious reason why touchesEnded would never be called? – bplattenburg Jul 01 '16 at 00:36
0

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.

RegularExpression
  • 3,531
  • 2
  • 25
  • 36
0

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
    }    
}
brandonscript
  • 68,675
  • 32
  • 163
  • 220
Yevhen Dubinin
  • 4,657
  • 3
  • 34
  • 57
-1

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.. }

}
ingconti
  • 10,876
  • 3
  • 61
  • 48