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

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

提交回复
热议问题