Reputation: 575
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
Upvotes: 10
Views: 13868
Reputation: 1610
UIGestureRecognizer *sliderTapview1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedImage:)];
[img addGestureRecognizer:sliderTapview1];
img.userInteractionEnabled = YES;
just replace that.
Upvotes: 3
Reputation: 1860
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.
Upvotes: 0
Reputation: 705
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 :)
Upvotes: 2
Reputation: 605
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");
}
Upvotes: 23
Reputation: 1141
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
Upvotes: 8