我从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);
}
答案 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];
我认为这会对你有所帮助。