4

我有一个UIView和一个Draggable UIImageView。背景颜色UIView绿色

当我拖动图像时,UIImageView将触摸UIView。当我将图像拖过时UIViewUIView应该变成红色

如何检查是否UIImageView达到了UIView

4

4 回答 4

8
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{   
    if (CGRectIntersectsRect(imageview.frame, view.frame)) {
        view.backgroundColor=[UIColor redcolor];
    }
}
于 2013-04-30T10:22:07.013 回答
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

于 2013-04-30T10:17:20.263 回答
1

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

在此处输入图像描述

https://github.com/wryphonedev/DragReaction

于 2013-10-25T13:00:18.420 回答
0

通过使用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) {

    }
}
于 2013-04-30T10:29:12.527 回答