如何找出触摸事件结束的视图?

我希望将UIImage拖放到其中一个UIButton上,然后根据我将其拖到哪个button来重新定位UIImage。

我碰到的问题是,UITouch只logging我的触摸开始的视图,我想访问我的触摸结束的视图。 我怎样才能做到这一点?

码:

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

这是很容易检测到你最后感动的看法,试试这个代码

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

使用命中testing。 你知道这个点的位置,作为一个CGPoint在最终的超级观点, self.view 。 那么你可以问self.view它的哪个子视图是在:

 CGPoint location = [touch locationInView:self.view]; UIView* v = [self.view hitTest:location withEvent:nil]; 

UIView v是你正在寻找的视图。

一般来说,你需要把握触摸结束的位置,并确定它是否位于你感兴趣的视图之内。我使用类似下面的代码来做到这一点:

 CGPoint newCenter = dragView.center; for (UIView *v in dropTargets) { CGRect convertedTargetFrame = [v.superview convertRect:v.frame toView:nil]; if (CGRectContainsPoint(convertedTargetFrame, newCenter)) { activeTargetIndex = idx; } } 

(这段代码是在编辑过程中去掉很多不相关的东西,但是我在dropTargets中保留了一个潜在的放置目标列表,这只是看着那个列表来看看哪个潜在的UIViews可能包含这个点)。

最重要的是,如果你知道视图或视图,你可能会拖动项目,那么你可以确定该视图的框架是否包含使用CGRectContainsPoint的点。 重要的是要记住,它们可能位于不同的坐标系中,在比较之前可能需要从一个坐标系转换到另一个坐标系。

假设你的容器中有一个@property的目标视图,并且目标视图被添加到容器中:

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