简体   繁体   中英

Detect tap on UIImageView IOS

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

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");

}

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

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 :)

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

   img.userInteractionEnabled = YES;

just replace that.

看起来每件事情都很好尝试这样做[myImage userInteractionEnabled:YES]并在图像视图的界面构建器中删除“自动布局”和“启用用户交互”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM