我希望将 UIImage 拖到多个 UIButton 之一上,并根据我将其拖到的按钮重新定位。
我遇到的问题是 UITouch 只记录我的触摸开始的 View ,我想访问我的触摸结束的 View 。我该怎么做?
代码:
-(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;
}
}
最佳答案
使用 HitTest 。根据最终 super View self.view
,您知道该点的位置作为 CGPoint。然后你可以问self.view
那个点在它的哪个 subview 中:
CGPoint location = [touch locationInView:self.view];
UIView* v = [self.view hitTest:location withEvent:nil];
UIView v
是您正在寻找的 View 。
关于ios - 如何找出触摸事件在哪个 View 结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16328875/