【问题标题】:how to check UIView and UIImageView is touched or not in iphone sdk?如何在 iphone sdk 中检查 UIView 和 UIImageView 是否被触摸?
【发布时间】:2013-04-30 10:14:59
【问题描述】:

我有一个 UIView 和一个 可拖动 UIImageViewUIView 的背景颜色是绿色

当我拖动图像时,UIImageView 将触摸UIView。 当我将图像拖到UIView 上时,UIView 的颜色应该变为红色

如何检查UIImageView 是否超过UIView

【问题讨论】:

  • 拖动UIImageView,你是用pangesture还是touches move方法???
  • @NishaSingh 我添加了当 UIImageView 移动到 yourView'frame 内时更改 yourView 背景颜色的代码,否则设置 clearColor,当图像移出 yourView 时,您可以设置任何颜色 :)跨度>

标签: iphone ios objective-c uigesturerecognizer uitouch


【解决方案1】:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{   
    if (CGRectIntersectsRect(imageview.frame, view.frame)) {
        view.backgroundColor=[UIColor redcolor];
    }
}

【讨论】:

  • 小心,我已经多次被否决,因为我没有在代码中添加解释。添加正在发生的事情的摘要以获得完整的答案。
【解决方案2】:

您可以使用touchesBegan 方法检查,如下所示...

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    [touch locationInView:self.view];
    if([touch.view isKindOfClass:[UIImageView class]])
    {
      ///This is UIImageView
    }
    else if([touch.view isKindOfClass:[UIView class]]) 
    {
      ///This is UIView
    }
}

当您移动UIImageView 时,它会使用以下代码更改UIView 的背景颜色...

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *tap = [touches anyObject];
    CGPoint pointToMove = [tap locationInView:self.view];
    if([tap.view isKindOfClass:[UIImageView class]])
    {
        UIImageView *tempImage=(UIImageView *) tap.view;
        if ([yourView pointInside:pointToMove withEvent:event])
        {
            [yourView setBackgroundColor:[UIColor redColor]];
        }
        else{
            [yourView setBackgroundColor:[UIColor clearColor]];//set backcolor which you want when `UIImageView` move outside of yourView 
        }
    }
}

对于搬家,请参阅此链接Move UIImage only inside of another UIImage的答案

【讨论】:

  • 您没有检查UIImageView 是否与UIView 相交。
  • @Mar0ux 你想说什么??如果您知道最佳答案,请发布您的答案..这里有什么问题?它的工作,我测试它..
  • 试试这个:触摸左上角的UIImageView并拖动它,使UIView的左上角接触UIImageView的右下角。请参阅Ashini's answer 了解如何完成。
  • 我完成了它并在之后它的工作我大部分代码的第二部分......它为我工作,伙计......
  • 嗨@ParasJoshi:我正在实施LongGestureRecognizer。我想更改用户通过长手势触摸的特定部分的颜色。你能帮我解决这个问题吗?
【解决方案3】:

看看这个链接,可能对你有帮助:)

https://github.com/wryphonedev/DragReaction

【讨论】:

    【解决方案4】:

    通过使用UIPanGestureRecognizer

    - (void)onDraggingLetter:(UIPanGestureRecognizer *)panGR {
    
     CGPoint translation = [panGR translationInView:self.view];
    
        if (panGR.state == UIGestureRecognizerStateBegan) {
    
        } else if (panGR.state == UIGestureRecognizerStateChanged) {  
    
            CGRect _rect = panGR.view.frame;
            _rect.origin.x = dragPoint.x + translation.x;
            _rect.origin.y = dragPoint.y + translation.y;
            panGR.view.frame = _rect;
    
             if (CGRectIntersectsRect(panGR.view.frame, greenView.frame)) {
    
                   greenView.backgroundColor=[UIColor redColor];
             }
    
        } else if (panGR.state == UIGestureRecognizerStateEnded) {
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-14
      • 2013-06-20
      • 1970-01-01
      • 2016-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多