简体   繁体   English

检测点击UIImageView IOS

[英]Detect tap on UIImageView IOS

Am I doing something wrong? 难道我做错了什么? I can't detect the tap on the UIImageview . 我无法检测到UIImageview上的点UIImageview The UIImageView *myImage is create in storyboard. UIImageView *myImage是在storyboard中创建的。 the code files are here : https://drive.google.com/folderview?id=0B2jWw-2wZC52NDBFZ2lXUTUzQXM&usp=sharing 代码文件位于: https//drive.google.com/folderview?id = 0B2jWw-2wZC52NDBFZ2lXUTUzQXM&usp =sharing

File: ViewController.h 文件: ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

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

@end

File: ViewController.m 文件: 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作为:

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 您只需将.h文件更改为

 #import <UIKit/UIKit.h>

 @interface ViewController : UIViewController<UIGestureRecognizerDelegate>

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

@end

Then .m file 然后是.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:)];
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 我将tapgestureRecognizer连接到委托。您只需尝试此代码

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 :) 另外,只是想一想,在分配tapgesture之前尝试在imageview上启用交互:)

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

   img.userInteractionEnabled = YES;

just replace that. 只是替换它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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