user798370
user798370

Reputation: 179

How to detect if image is touched

How can you detect if an image is touched in Xcode? I have looked at Apple documentation and it is really confusing... I saw:

if (CGRectContainsPoint([imageView frame], location))

but my image still won't move. I tried using touchesBegan + touchesMoved, and set the image's userInteractionIsEnabled to YES, but it still won't detect it :(


EDIT: Thank you all so much for your great suggestions! In the end, I really wanted to make it as simple as possible, and I knew that my code should work, so I kept fiddling with it, and after a good night's sleep, I realized it was a fairly simple solution:

In my touchesMoved:

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];

    CGRect shapeRect = [imageView frame];
    CGRect dropSquareRect = [dropSquare frame];

    CGPoint touchLocation = CGPointMake(location.x, location.y);

    if (CGRectContainsPoint(shapeRect, touchLocation))
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:.3];
        [imageView setCenter:touchLocation];
        [UIView commitAnimations];
    }

    if (CGRectIntersectsRect(shapeRect, dropSquareRect))
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:.3];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        self.imageView.alpha = 0;
        self.imageView.center = CGPointMake(dropSquare.center.x, dropSquare.center.y);
        self.imageView.transform = CGAffineTransformMakeScale(0.8, 0.8);
        [UIView commitAnimations];

Upvotes: 9

Views: 11675

Answers (6)

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31730

you could consider using UITapGestureRecognizer with UIImageView to detect the touches.

And also not forget to set userInteractionIsEnabled to YES.

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self              action:@selector(imageTapped:)];
myImageView.userInteractionIsEnabled = YES;
[myImageView addGestureRecognizer:tap];
[tap release];

Implement imageTapped: method.

- (void )imageTapped:(UITapGestureRecognizer *) gestureRecognizer 
   {

   }

Upvotes: 13

Gypsa
Gypsa

Reputation: 11314

You can try this Suppose you have an

IBOutlet UIImageView *touchImageVIew;
Let touchImageVIew height and width are 50,25;

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    // Retrieve the touch point

    CGPoint pt = [[touches anyObject] locationInView:touchImageVIew];

    if (pt.x>=0 && pt.x<=50 && pt.y>=0 && pt.y<=25) {
        NSLog(@"image touched");
    }

    else 
{
NSLog(@"image not touched");
}
}

Adjust height,width and name according to your requirement.

Upvotes: 3

Nithin
Nithin

Reputation: 6475

If you are not so particular in using UIImageView, try using a custom button with your image in it and use the action methods, touchDown and touchDraggedOut for moving the image.

Upvotes: 2

rptwsthi
rptwsthi

Reputation: 10182

You Can use this:

Do Following setting in view did load:

UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[rightRecognizer setNumberOfTouchesRequired:1];
[mainSlideShowImageScrollView addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[leftRecognizer setNumberOfTouchesRequired:1];
[mainSlideShowImageScrollView addGestureRecognizer:leftRecognizer];
[leftRecognizer release];   

Then Use following methods:

- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
   {
      //Do moving
   }

- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
  {
      // do moving
  }

Upvotes: 2

PatrickG4
PatrickG4

Reputation: 53

You need the touchesBegan method e.g.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint myPoint = [touch locationInView:self];
   if (CGRectContainsPoint([imageView frame], location)){
    // code here             
   }
}

I just read that you tried it, although I am not sure why it doesn't work. Try using the tap gesture as suggested below.

Upvotes: 3

Jamie
Jamie

Reputation: 5122

You could add a UIPanGesureRecognizer to the UIImageView that's holding the UIImage. This will allow you to detect when the user is panning on the image and move the images translation accordingly. The reference to the documentation is here.

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPanGestureRecognizer_Class/Reference/Reference.html

It's nice using the gesture recognizers since it keeps the consistency with the rest of the OS as far as panning goes.

Hope this helps.

Upvotes: 3

Related Questions