我有一个UIView和一个Draggable UIImageView。背景颜色UIView为绿色。
当我拖动图像时,UIImageView将触摸UIView。当我将图像拖过时UIView,UIView应该变成红色。
如何检查是否UIImageView达到了UIView?
我有一个UIView和一个Draggable UIImageView。背景颜色UIView为绿色。
当我拖动图像时,UIImageView将触摸UIView。当我将图像拖过时UIView,UIView应该变成红色。
如何检查是否UIImageView达到了UIView?
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{   
    if (CGRectIntersectsRect(imageview.frame, view.frame)) {
        view.backgroundColor=[UIColor redcolor];
    }
}
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
通过使用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) {
    }
}