2

我在从 imageview 获取标签号时遇到了麻烦。我将 UIGestureRecognizer 添加到滚动视图中的每个图像视图中,并尝试从触摸视图中获取标签号。在我触摸图像后,什么都没有发生。我的代码错了吗?请帮我。

    // to add images
    for (int i = 0; i < app.arrPhotoList.count; i++) {

        UIImageView *subImg = [[UIImageView alloc]initWithFrame:frame];
        subImg.contentMode = UIViewContentModeScaleAspectFit;
        subImg.layer.borderColor = [UIColor blackColor].CGColor;
        subImg.layer.borderWidth = 2.0f;
        subImg.tag = i;
        subImg.userInteractionEnabled = YES;
        UIGestureRecognizer *singleTab = [[UIGestureRecognizer alloc]initWithTarget:self action:@selector(imageTag:)];
        [subImg addGestureRecognizer:singleTab];


        /////// add img to array of imageviews/////////////
        NSString *imgName = [[app.arrPhotoList objectAtIndex:i]stringByAppendingString:@"_thumb.jpg"];
        subImg.image = [UIImage imageWithContentsOfFile:imgName];
        [scrImgView addSubview:subImg];
}


-(void) imageTag:(UIGestureRecognizer *)sender{
    NSLog(@"you selected tag number is : %d",sender.view.tag);
}
4

2 回答 2

0

更改此行

UIGestureRecognizer *singleTab = [[UIGestureRecognizer alloc]initWithTarget:self action:@selector(imageTag:)];

对此

UITapGestureRecognizer *singleTab = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTag:)];

HTH。

于 2012-07-06T06:47:48.550 回答
0

您应该编写UITapGestureRecognizer而不是UIGestureRecognizer设置委托。有关UIGestureRecognizer的更多信息。

        UITapGestureRecognizer *singleTab = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTag:)];
        singleTab.delegate=self;
        [subImg addGestureRecognizer:singleTab];

我想这会对你有所帮助。

于 2012-07-06T06:49:09.420 回答