我有一个背景图像(imageView - 标签0),它已被几个标记的图像子视图(imgView - 标签1 - ?)填充。我在主背景图像上添加了轻击识别器,并且还尝试了子视图。我可以通过轻击来到背景轻拍识别器例程,但无法识别哪个子视图已被轻敲。识别哪个子视图被挖掘
添加自来水识别为背景(在视图做负载):
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
[imageView addGestureRecognizer:singleTap];
[imageView setUserInteractionEnabled:YES];
添加自来水识别到每个子视图,因为它被装载:
- (void) showPics{ // display previously stored pictures
NSString *PictureName;
NSNumber *x;
NSNumber *y;
UIImage *viewImage;
tag = 1; // NSInteger declared in .h - background tag is zero
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
NSLog(@"reading from picFile: %@",self.picFile);
if ([[NSFileManager defaultManager] fileExistsAtPath:self.picFile]){
NSLog(@"FILE Found");
NSMutableArray *picData;
picData = [[NSMutableArray alloc] initWithContentsOfFile:self.picFile];
int count = picData.count/3;
NSLog(@"Count: %d",count);
// set up picture path before cycling through array
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// cycle to load pictures from file and place on background image
for(int i = 0; i < count; i++) {
PictureName = [picData objectAtIndex:i * 3];
NSString *showPic=[documentsDirectory stringByAppendingPathComponent:PictureName];
if ([[NSFileManager defaultManager] fileExistsAtPath:showPic]){
NSLog(@"Loading: %@",PictureName);
}else{
NSLog(@"Picture Does Not Exist");
}
x = [picData objectAtIndex:i * 3 + 1]; // x coord
y = [picData objectAtIndex:i * 3 + 2]; // y coord
viewImage = [UIImage imageWithContentsOfFile:showPic];
// find center of displayed image and place picture there
imgView = [[[UIImageView alloc] initWithFrame:CGRectMake([x floatValue] - 225,[y floatValue] - 172.5, 450, 345)] autorelease];
[imgView setImage:viewImage];
[imgView setContentMode:UIViewContentModeCenter];
[imgView addGestureRecognizer:singleTap];
[imgView setUserInteractionEnabled:YES];
imgView.tag = tag;
[imageView addSubview:imgView];
tag++;
NSLog(@"Tag: %d",tag);
}
[picData release];
}else{
NSLog(@"Did not find File");
}
}
被调用的子程序一个水龙头的任何地方:
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)sender{
NSLog(@"singleTapGestureCaptured - SUBVIEWS: %d", [imageView.subviews count]);
NSLog(@"TAG: %d",sender.view.tag);
}
这正确地给了我的子视图的数量,但alwa ys返回“TAG:0”,除非我点击最后添加的子视图。我需要知道的是访问被点击的子视图TAG的正确格式,所以我可以使其处于活动状态并移动它,或者有关如何执行此操作的任何其他建议。
这是我喜欢的数据与观点,而不是相关联的情况下,使用标签并且必须稍后通过标签进行查找。你可以使用'objc_setAssociatedObject()'来做到这一点。 – nielsbot