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

后端 未结 5 1527
伪装坚强ぢ
伪装坚强ぢ 2020-12-06 00:16

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

相关标签:
5条回答
  • 2020-12-06 00:49

    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.

    0 讨论(0)
  • 2020-12-06 00:50

    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);
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-06 00:59

    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
        }    
    }
    
    0 讨论(0)
  • 2020-12-06 01:00

    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.

    0 讨论(0)
  • 2020-12-06 01:04

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

    }
    
    0 讨论(0)
提交回复
热议问题