ios - 如何找出触摸事件在哪个 View 结束?

标签 ios objective-c touch uitouch

我希望将 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/

相关文章:

ios - 更新 Firebase 中的数据会删除旧子项并使用 SWIFT 替换它

objective-c - iOS - 加载自定义表格单元格导致第二次加载出错

javascript - 使用 JavaScript 在 Canvas 上描绘一条线

javascript - 移动 safari 上的 touchMove 事件 - 导致整个页面被拖动

button - cocos2d通过按钮扩展触摸区域

ios - 保留 iOS 应用名称

ios - AFHTTPSessionManager - 获取未序列化/原始响应正文(NSData?)

ios - 将 NSString 从 plist 传递到 ImageView 时遇到问题

objective-c - 如何从 Objective-C 代码中提取 AST?

ios - 仪器应用程序无法打开 tracetemplate 文件