ios - 检测点击 UIImageView IOS

标签 ios imageview uitapgesturerecognizer

我做错了什么吗?我无法检测到 UIImageview 上的点击。 UIImageView *myImage 是在 Storyboard中创建的。代码文件在这里:https://drive.google.com/folderview?id=0B2jWw-2wZC52NDBFZ2lXUTUzQXM&usp=sharing

文件:ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

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

@end

文件: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

最佳答案

我认为你应该使用类 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");

}

关于ios - 检测点击 UIImageView IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22629645/

相关文章:

java - Android ImageView - 填充宽度和调整大小以保持纵横比

java - 使用文件路径时,Arraylist 无法识别照片

ios - 点击时更改 TableView 单元格的 UIImage

ios - 如果目标是普通类,UITapGestureRecognizer 无法正常工作

ios - UIViewController 基类——好不好

javascript - 使用 contenteditable div 时隐藏移动设备上的软键盘

ios - XMPPFramework-TURNSocket无法接收到我自己发送的数据吗?

iOS 7 背景模式 - 不适用于 App Store(无规则适用)

android - 为 ImageView 重复调用时,适用于 Android 的 Picasso 库行为不一致

ios - 如何使用 UITapGestureRecognizer 调用 didSelectRowAtIndexPath 方法?