19

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

我遇到的问题是 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;
    }

}
4

5 回答 5

23

使用命中测试。就终极超视图而言,您知道该点的位置为 CGPoint self.view,. 然后您可以询问self.view该点在哪个子视图中:

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

UIViewv是您正在寻找的视图。

于 2013-05-02T02:49:10.947 回答
17

很容易检测到你最终触摸的视图,试试这个代码

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

    }

}
于 2013-05-02T02:30:29.913 回答
0

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

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 中保留了一个潜在放置目标的列表,这只是查看该列表以查看哪些潜在的 UIView 可能包含该点)。

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

于 2013-05-02T02:34:25.710 回答
0

假设@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
    }    
}
于 2016-02-11T12:52:34.380 回答
-1

迅捷4:

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

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

// 继续.. }

}
于 2018-02-14T18:41:10.343 回答