Question

Am I doing something wrong? I can't detect the tap on the UIImageview. The UIImageView *myImage is create in storyboard. the code files are here : https://drive.google.com/folderview?id=0B2jWw-2wZC52NDBFZ2lXUTUzQXM&usp=sharing

File: ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *myImage;

@end

File: ViewController.m

#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIGestureRecognizer *tapGestureRecognizer = [[UIGestureRecognizer alloc] initWithTarget:self action:@selector(tappedImage:)];
    [self.myImage addGestureRecognizer:tapGestureRecognizer];
    self.myImage.userInteractionEnabled = YES; // default is no for UIImageView

}

- (void)tappedImage:(UIGestureRecognizer *)gestureRecognizer {
    //UIImageView *myImage = (UIImageView *)gestureRecognizer.view;
    // do stuff;
    NSLog(@"it works");
}


@end
Was it helpful?

Solution

I think you should use class UITapGestureRecognizer as:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)];
singleTap.numberOfTapsRequired = 1;
preArrowImage.userInteractionEnabled = YES;
[preArrowImage addGestureRecognizer:singleTap];

-(void)tapDetected{
NSLog(@"single Tap on imageview");

}

OTHER TIPS

You just change the .h file to

 #import <UIKit/UIKit.h>

 @interface ViewController : UIViewController<UIGestureRecognizerDelegate>

@property (strong, nonatomic) IBOutlet UIImageView *myImage;

@end

Then .m file

 #import "ViewController.h"
 @interface ViewController ()

 @end

 @implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

UIGestureRecognizer *tapGestureRecognizer = [[UIGestureRecognizer alloc] initWithTarget:self action:@selector(tappedImage:)];
tapgestureRecognizer.delegate = self;
[self.myImage addGestureRecognizer:tapGestureRecognizer];
self.myImage.userInteractionEnabled = YES; // default is no for UIImageView

}

 - (void)tappedImage:(UIGestureRecognizer *)gestureRecognizer {
//UIImageView *myImage = (UIImageView *)gestureRecognizer.view;
// do stuff;
NSLog(@"it works");
 }


@end

I connect tapgestureRecognizer to delegate.You just try this code

 UIGestureRecognizer *sliderTapview1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedImage:)];
    [img addGestureRecognizer:sliderTapview1];

   img.userInteractionEnabled = YES;

just replace that.

even though you enabled it by code, try to go to your storyboard check the "enable user interaction" on the image view, your code seems fine, try that and let me know.

plus, just a thought, try to enable the interaction on the imageview before you assign the tapgesture :)

looks every thing is fine try to do like this [myImage userInteractionEnabled:YES] and remove "auto layout" and "enable user interaction" in interface builder on image view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top