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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16328875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:24:33  来源:igfitidea点击:

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

iosobjective-ctouchuitouch

提问by Declan McKenna

I wish to drag a UIImage on to one of several UIButtons and have it repositioned based on which button I drag it to.

我希望将 UIImage 拖到几个 UIButton 之一上,并根据我将它拖到的按钮重新定位。

The problem I've ran in to is that UITouch only records the view where my touch began, I'd like to access the view my touch ends at. How can I do this?

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

Code:

代码:

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

}

采纳答案by Thilina Chamin Hewagama

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

    }

}

回答by matt

Use hit-testing. You know the location of this point as a CGPoint in terms of the ultimate superview, self.view. Then you can ask self.viewwhich of its subviews that point is in:

使用命中测试。你知道这个点的位置作为 CGPoint 在最终超级视图方面,self.view。然后您可以询问self.view该点在其哪个子视图中:

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

The UIView vis the view you're looking for.

UIViewv是您正在寻找的视图。

回答by RegularExpression

In general, you need to take the point where the touch ended, and determine whether it lies within the view you're interested in. I do this using code similar to the following:

通常,您需要获取触摸结束的点,并确定它是否位于您感兴趣的视图内。我使用类似于以下的代码来执行此操作:

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

(this code was edited on the fly to take out a lot of irrelevant stuff, but I keep a list of potential drop targets in dropTargets, and this is just looking at that list to see which of the potential UIViews might contain the point).

(此代码经过即时编辑以删除许多不相关的内容,但我在 dropTargets 中保留了潜在放置目标的列表,这只是查看该列表以查看哪些潜在的 UIView 可能包含该点)。

The essential thing is that if you know the view or views you're potentially dragging the item to, then you can determine whether that view's frame contains the point using CGRectContainsPoint. The important thing is to keep in mind they may lie in different coordinate systems and it may be necessary to convert from one to the other before your comparison.

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

回答by Yevhen Dubinin

Assuming there is a @propertyof the target view in your container, and the target view is added to the container:

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

回答by ingconti

swift 4:

快速4:

override func touchesEnded(_ touches: Set, with event: UIEvent?) {

覆盖 func touchesEnded(_ touches: Set, with event: UIEvent?) {

    if let touch = touches.first{
        let location = touch.location(in: self.view)
        let v = touch.view

// go on.. }

// 继续.. }

}