2013-04-30 70 views
4

我有一个UIView和一个DraggableUIImageViewUIView的背景颜色是绿色如何检查UIView和UIImageView被触摸或不在iPhone SDK?

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

如何检查UIImageView达到UIView

+0

对于拖动'UIImageView',您使用的pangesture或触摸移动方法???? – Venkat 2013-04-30 10:17:44

+1

请看我的回答 – Ashini 2013-04-30 10:24:16

+0

@NishaSingh我添加的代码改变了你的视图的背景颜色当UIImageView被移动到你的视图框内,否则其设置clearColor,你可以设置任何颜色时,该图像移动你的视图:) – 2013-04-30 11:17:19

回答

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波纹管代码BACKGROUNDCOLOR ...

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

+1

你不检查'UIImageView'是否与'UIView'相交。 – Mar0ux 2013-04-30 13:37:58

+0

@ Mar0ux你想说什么?也如果你知道最好的答案,然后张贴你的答案..这里最新错误?它的工作和我测试.. – 2013-05-01 04:04:39

+1

试试这个:触摸左上角的'UIImageView'并拖动它,使'UIView'的左上角触及'UIImageView'的右下角。请参阅[Ashini的答案](http://stackoverflow.com/a/16297553/440060)了解它应该如何完成。 – Mar0ux 2013-05-01 04:08:18

8
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    if (CGRectIntersectsRect(imageview.frame, view.frame)) { 
     view.backgroundColor=[UIColor redcolor]; 
    } 
} 
+0

+1好答案。 – Bhavin 2013-04-30 11:18:34

+0

小心翼翼,我因为没有向代码添加解释而多次投了票。为完整答案添加所发生事件的摘要。 – 2013-04-30 13:28:00

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) { 

    } 
}