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

后端 未结 5 1544
伪装坚强ぢ
伪装坚强ぢ 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: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
        }    
    }
    

提交回复
热议问题