使用点击手势创建ImageView列表时遇到问题。当我创建手势时,不会调用选择器功能。当我创建只有一个imageView函数被调用。知道为什么吗?这是一个大型滚动视图的子视图。
这是我的代码:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTaped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
for(int k=0;k<MyList.count;k++){
for(int i=0;i<listSize;i++){
UIImageView *clickForDetail =[[UIImageView alloc]initWithFrame:CGRectMake(i*HELLO_WIDTH,k*LIST_ROW_HEIGH ,HELLO_WIDTH, LIST_ROW_HEIGH)];
clickForDetail.backgroundColor = [UIColor clearColor];
clickForDetail.tag = tag;
clickForDetail.userInteractionEnabled = YES;
[clickForDetail addGestureRecognizer:singleTap];
[myScroll addSubview:clickForDetail];
tag++;
}
}
和选择器功能:
-(void)imageTaped: (UITapGestureRecognizer *)recognizer
{
NSLog(@"single Tap on imageview");
}
是否有可能以某种方式获取被点击的ImageView的标签?
答案 0 :(得分:2)
您需要为每个视图对象添加不同的点击手势
for(int k=0;k<MyList.count;k++){
for(int i=0;i<listSize;i++){
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTaped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
UIImageView *clickForDetail =[[UIImageView alloc]initWithFrame:CGRectMake(i*HELLO_WIDTH,k*LIST_ROW_HEIGH ,HELLO_WIDTH, LIST_ROW_HEIGH)];
clickForDetail.backgroundColor = [UIColor clearColor];
clickForDetail.tag = tag;
clickForDetail.userInteractionEnabled = YES;
[clickForDetail addGestureRecognizer:singleTap];
[epgScroll addSubview:clickForDetail];
tag++;
}
}
-(void)imageTaped: (UITapGestureRecognizer *)recognizer
{
NSLog(@"single Tap on imageview");
UIImageView *selectedTextView = (UIImageView *)recognizer.view;
}