Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check UIView and UIImageView is touched or not in iphone sdk?

I have One UIView and one Draggable UIImageView. Background color of UIView is green.

When I will drag the image, UIImageView will touch UIView. When I drag the image over UIView the colour of UIView should become red.

How to check that UIImageView reached over UIView ?

like image 638
Nisha Singh Avatar asked Dec 21 '22 06:12

Nisha Singh


2 Answers

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{   
    if (CGRectIntersectsRect(imageview.frame, view.frame)) {
        view.backgroundColor=[UIColor redcolor];
    }
}
like image 136
Ashini Avatar answered Dec 22 '22 19:12

Ashini


you can check that with touchesBegan method like bellow...

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

and when you move the UIImageView at that time its change the backGroundColor of UIView with bellow code...

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

Also For Moving see the answer from this link Move UIImage only inside of another UIImage

like image 25
Paras Joshi Avatar answered Dec 22 '22 20:12

Paras Joshi