UIImageView iOS Touch Detection

Am I doing something wrong? I can not detect the crane on UIImageview . UIImageView *myImage is created in the storyboard. 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 
+6
source share
5 answers

I think you should use the UITapGestureRecognizer class 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"); } 
+17
source

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 am connecting tapgestureRecognizer to a delegate. You just try this code

+8
source

even if you turned it on by code, try switching to your storyboard to check "enable user interaction" in the image view, your code seems beautiful, try it and let me know.

plus, just think, try turning on interaction in the image view before you assign tapgesture :)

+2
source
  UIGestureRecognizer *sliderTapview1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedImage:)]; [img addGestureRecognizer:sliderTapview1]; img.userInteractionEnabled = YES; 

just replace it.

+2
source

Everything that needs to be done looks. [myImage userInteractionEnabled: YES] and remove the "automatic layout" and "enable user interaction" in the interface builder when viewing images.

0
source

All Articles