如何从scrollview中的imageview获取标签号?

时间:2012-07-06 06:01:15

标签: xcode tags touch imageview scrollview

我从imageview获取标签号时遇到了麻烦。 我在scrollview中为每个图像视图添加了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);
}

2 个答案:

答案 0 :(得分:0)

更改此行

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

到这个

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

HTH。

答案 1 :(得分:0)

您应该写UITapGestureRecognizer而不是UIGestureRecognizer并设置委托。有关UIGestureRecognizer

的任何进一步信息
        UITapGestureRecognizer *singleTab = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTag:)];
        singleTab.delegate=self;
        [subImg addGestureRecognizer:singleTab];

我认为这会对你有所帮助。