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?
Adding a Tap Gesture Recognizer in Interface Builder You don't need to switch between the code editor and Interface Builder. Open Main. storyboard and drag a tap gesture recognizer from the Object Library and drop it onto the view we added earlier. The tap gesture recognizer appears in the Document Outline on the left.
Open the Library, look for "Tap Gesture Recognizer" object. Drag the object to your storyboard, and set the delegate to the image you want to trigger actions.
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...
}
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With