如何找出查看触摸事件在结束?(How to find out what view a touch e

2019-09-01 16:59发布

我想拖一个UIImage上几个UIButtons之一,并把它重新定位,根据我拖累了它的按钮。

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

码:

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

}

Answer 1:

这是很容易检测到您的最终打动观点,试试这个代码

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

    }

}


Answer 2:

使用命中测试。 你知道这点作为一个CGPoint在最终上海华,计位置self.view 。 然后,你可以问self.view其子视图的点是:

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

UIView的v是你要找的看法。



Answer 3:

在一般情况下,你需要采取在触摸结束点,并确定它是否位于您感兴趣的视图中我做到这一点使用代码类似于以下内容:

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点。 最重要的是要记住,他们可能位于不同的坐标系,它可能有必要从一个你比较之前转换为其他。



Answer 4:

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


Answer 5:

SWIFT 4:

覆盖FUNC touchesEnded(_触动:设置,与事件:?的UIEvent){

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

// 继续.. }

}


文章来源: How to find out what view a touch event ended at?