2012-05-07 44 views
1

我有一个视图控制器,其中显示图像的网格/数组,其中每个图像视图都是自定义笔尖(自定义笔尖,因为图像的名称为&喜欢/不喜欢的图标)。所以我在我的视图控制器viewDidLoad中显示了像这样的图像网格。iOS手势处理:在使用自定义UIView时添加手势识别器的位置

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 
} 

现在我需要为屏幕上显示的每个图像添加一个水龙头手势识别器。在这种情况下,在自定义笔尖/类中添加手势识别器CustomImageView似乎符合逻辑。 CustomImageView延伸UIView,所以它似乎无法在此处声明手势识别器(自动完成不会出现,语法高亮也不起作用)。我在这里错过了什么?

回答

5

您可以将一个手势识别器添加到您的CustomImageView(只要它是UIView)。尝试是这样的:

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

[imgView addGestureRecognizer:tapRecognizer]; 

需要注意的是,你应该看到自动完成的唯一方法是addGestureRecognizer

通常,为了决定某个功能是否存在,通常需要官方文档(或编译器,如果您喜欢)自动完成。根据我的经验,自动完成并不总是正确的。

+0

+1谢谢塞尔吉奥。这工作。感谢有关自动完成的提示会牢记在心。 :) – vikmalhotra