4

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 ?

Bhavin
  • 27,155
  • 11
  • 55
  • 94
Nisha Singh
  • 143
  • 4
  • 12
  • For dragging `UIImageView`, are you using pangesture or touches move method???? – Venk Apr 30 '13 at 10:17
  • @NishaSingh i add code which changed color of yourView's backcolor when UIImageView is moved inside of yourView'frame otherwise its set clearColor,you can set any color when that image move outside of that yourView :) – Paras Joshi Apr 30 '13 at 11:17

3 Answers3

8
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{   
    if (CGRectIntersectsRect(imageview.frame, view.frame)) {
        view.backgroundColor=[UIColor redcolor];
    }
}
rptwsthi
  • 10,094
  • 10
  • 68
  • 109
Ashini
  • 492
  • 3
  • 11
  • Careful, I've been down voted numerous times for not adding an explanation to the code. Add a summary of what's happening for a complete answer. – Mark McCorkle Apr 30 '13 at 13:28
2

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

Community
  • 1
  • 1
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • 1
    You are not checking if the `UIImageView` intersects with `UIView`. – maroux Apr 30 '13 at 13:37
  • @Mar0ux what you want to say?? and also if you know best answer then post your answer .. here whats wrong?? its working and i test it.. – Paras Joshi May 01 '13 at 04:04
  • 1
    Try this: touch the `UIImageView` from top left corner and drag it such that `UIView`'s top left touches `UIImageView`'s bottom right. See [Ashini's answer](http://stackoverflow.com/a/16297553/440060) for how it should be done. – maroux May 01 '13 at 04:08
  • i completed it and its working after that i most that second part of code ... its working for me dude... – Paras Joshi May 01 '13 at 04:16
  • Hi @ParasJoshi: I am implementing LongGestureRecognizer. I want to change the color of a specific portion which user touches via long gesture. Can you please help me with this? – Manthan Mar 10 '14 at 07:02
  • I can show you my code also if you can come in chat. – Manthan Mar 10 '14 at 07:03
0

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

    }
}
Venk
  • 5,949
  • 9
  • 41
  • 52