13

I am trying to make clickable UIImage, where the user can click it then it'll animate...

i am working with the UIScrollVIew that's why i used the UITapGesture instead of touchesBegan, and it seems that UIGestureRecognizer is not compatible with UIImage...

am i right?

i keep receiving this error message

receiver type 'UIImage' for instance message does not declare a method with selector 'addGestureRecognizer'

is there any other way?

Crisn
  • 243
  • 1
  • 3
  • 12
  • possible duplicate of [GestureRecognizer on UIImageView](http://stackoverflow.com/questions/3907397/gesturerecognizer-on-uiimageview) – iosMentalist Jun 09 '14 at 07:00

4 Answers4

27

You have to add TapGesture in UIImageView not UIImage

imgView.userInteractionEnabled = YES;

UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(tapGesture:)];

tapGesture1.numberOfTapsRequired = 1;

[tapGesture1 setDelegate:self];

[imgView addGestureRecognizer:tapGesture1];

[tapGesture1 release];

You can response to the tap with the defined selector and do stuff there

- (void) tapGesture: (id)sender
{
    //handle Tap...
 }
Pooja Jalan
  • 604
  • 7
  • 9
6

You have to add the gesture to UIImageView, not UIImage

jasondinh
  • 918
  • 7
  • 21
  • it's not working with Core Animation. what i'm trying to do here is that, the image will animate when you click it. – Crisn Feb 09 '12 at 06:36
  • The point here is that UIImage cannot receive touch event. You have to handle the touch event somewhere else. – jasondinh Feb 09 '12 at 06:40
3

You can simply add a TapGestureRecognizer to a UIImageView. You have to use a UIImageView because gesture recognizer are only allowed to be added to views.

UIView *someView = [[UIView alloc] initWithFrame:CGRectZero];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
tapRecognizer.numberOfTapsRequired = 1;
[someView addGestureRecognizer:tapRecognizer];

You can response to the tap with the defined selector and do stuff there

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    // do stuff
}
mariusLAN
  • 1,195
  • 1
  • 12
  • 26
1

Try with UIButton instead of UIIMage and make the UIButton type custom. And on clicking the same you can show the animation.

Yama
  • 2,649
  • 3
  • 31
  • 63
  • can UIButton be animated? i used the UIImage because of CoreAnimation. – Crisn Feb 09 '12 at 06:35
  • Thats only a workaround but not the right answer. The Best way is to add the gesture to UIImageView. Don't abuse buttons as Images that's not the right way to do it. – mariusLAN Feb 09 '12 at 07:04
  • @mariusLAN : you can share your idea as an answer. – Yama Feb 09 '12 at 07:06
  • @mariusLAN : I'd also suggest that you justify your answer. There are many ways to solve any problem, but UIButtons are designed to trigger events when the user touches a location screen. That's what the Crisn is asking for. – Axeva Dec 18 '12 at 17:34