iOS手势处理:使用自定义UIView时添加手势识别器的位置

vik*_*tra 1 uigesturerecognizer ios5

我有一个视图控制器,我在其中显示一个网格/图像数组,其中每个图像视图都是一个自定义笔尖(自定义笔尖,因为图像也有一个名称和喜欢/不喜欢的图标).所以我在视图控制器中显示了这样的图像网格viewDidLoad.

int row=0, col=0;
for (int i=0; i<arrayImg.count; i++) {
    NSArray *topObj = [[NSBundle mainBundle] loadNibNamed:@"CustomImageView" owner:nil options:nil];
    CustomImageView *imgView = [topObj objectAtIndex:0];
    imgView.frame = CGRectMake(180*col+10, 180*row+10, 170, 170);

    // custom image values inserted here

    [self.view addSubView:imgView];

    // update the row,col variables here
}
Run Code Online (Sandbox Code Playgroud)

现在我需要为屏幕上显示的每个图像添加一个轻击手势识别器.CustomImageView在这种情况下,在自定义nib /类中添加手势识别器似乎是合乎逻辑的.CustomImageView扩展UIView,因此似乎无法在此处声明手势识别器(自动完成不会出现,语法突出显示也不起作用).我在这里错过了什么?

ser*_*gio 5

您肯定可以为您添加手势识别器CustomImageView(前提是它UIView).尝试这样的事情:

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];

[imgView addGestureRecognizer:tapRecognizer];
Run Code Online (Sandbox Code Playgroud)

请注意,您应该看到自动完成的唯一方法是addGestureRecognizer.

通常,更喜欢官方文档(或编译器,如果您愿意)而不是自动完成,以决定是否存在功能.根据我的经验,自动完成并不总是正确的.