Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
like image 797
user798370 Avatar asked Jul 05 '11 03:07

user798370


4 Answers

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 
   {

   }
like image 55
Jhaliya - Praveen Sharma Avatar answered Nov 05 '22 05:11

Jhaliya - Praveen Sharma


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.

like image 41
Jamie Avatar answered Nov 05 '22 03:11

Jamie


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.

like image 3
PatrickG4 Avatar answered Nov 05 '22 05:11

PatrickG4


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.

like image 3
Gypsa Avatar answered Nov 05 '22 04:11

Gypsa