Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Touch Up Inside for a UIImageView

Tags:

I'm trying to implement touch event similar to a Touch Up Inside a UIButton. I've seen some codes using touchesBegan:withEvent but it looks I need to slide a bit my finger over the screen. I actually just want to touch in the image

Code:

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {        NSLog(@"Image touched!"); } 

Regards!

like image 316
Claudio Avatar asked Jun 16 '11 13:06

Claudio


2 Answers

You could use the gesture to get the touch on UIImageView.

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]                          initWithTarget:self action:@selector(ClickEventOnImage:)];         [tapRecognizer setNumberOfTouchesRequired:2];         [tapRecognizer setDelegate:self];         //Don't forget to set the userInteractionEnabled to YES, by default It's NO.         myImageView.userInteractionEnabled = YES;         [myImageView addGestureRecognizer:tapRecognizer]; 

Implement the ClickEventOnImage function.

-(void) ClickEventOnImage:(id) sender {  } 
like image 143
Jhaliya - Praveen Sharma Avatar answered Oct 15 '22 09:10

Jhaliya - Praveen Sharma


UIImageView is notorious regarding this as its userInteractionEnabled is set to NO by default. You will have to enable it by doing

imageView.userInteractionEnabled = YES; 

And then you can use gesture recognizers or the normal touches* methods to handle touches.

like image 37
Deepak Danduprolu Avatar answered Oct 15 '22 10:10

Deepak Danduprolu