2013-10-21 111 views
0

我有一个背景图像(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的正确格式,所以我可以使其处于活动状态并移动它,或者有关如何执行此操作的任何其他建议。

+0

这是我喜欢的数据与观点,而不是相关联的情况下,使用标签并且必须稍后通过标签进行查找。你可以使用'objc_setAssociatedObject()'来做到这一点。 – nielsbot

回答

0

我不敢肯定地说,但我猜想的一点是你的tag变量不会改变。如果tag变量对于整个“.m”文件不是全局变量,那么它就不可能将它的值保存在不同的函数或方法中。尝试在“.h”文件中声明它并给它一些其他名称(例如Tag或tagValue),以便编译器可以区分应该返回的内容(变量或变量的标记),然后添加self.Tagself.tagValue(无论你怎样命名它)每次你把它分配给一个int。祝你好运,让我知道它是否有效。

+0

标记变量在.h中声明,并在添加子视图的循环中递增。我用这个来检查: NSArray * subviews = [imageView subviews]; (子视图中的UIView *子视图){ NSLog(@“%@”,subview); } 并且数据全部存在,包括我分配的标签。 –

0

放一个断点/ NSLog标记imgView的值。代码片段不会告诉您的变量(tag ++)的范围。它似乎没有得到更新。

imgView.tag = tag; 

注意,在singleTapGestureCaptured方法sender.view应该总是给你最顶层imgView(子视图),因为您要添加TapGestureRecognizer所有imgViews(子视图)。通常,当最顶层的子视图在这些情况下不添加(注册)任何手势时,手势依次传递到底层子视图/父视图,直到有人在视图堆栈中处理它。

编辑:(后额外的代码发布)

您需要创建单独的UITapGestureRecognizer对象,并将它们添加到每个子视图。但是,他们都可以使用相同的@selector方法。

因此,移动这条线for环..

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)]; 

略高于

 [imgView addGestureRecognizer:singleTap];  
+0

子视图被添加到一个循环中,并且标记值是正确的。我检查了以下子视图:NSArray * subviews = [imageView子视图]; for(UIView * subview in subviews){NSLog(@“%@”,subview); }并且数据都在那里,包括我分配的标签。 “sender.view.tag”部分有问题。我知道这是可以做到的,因为大约有一半的应用似乎可以让你通过触摸来移动或操作图像,而且它们必须是背景图像的子视图。 –

+0

你在'sender.view'(imgView或imageView或其他?)中获得了什么?输出这个'NSLog(@“sender.view =%@”,sender.view)'。此外,如果您可以放置​​更多的代码,特别是与“addsubview”和“addGestureRecognizer”相关的所有调用,这将会有所帮助。 – Ashok

+0

你对最上面的子视图是正确的。如果我点击添加的最后一张图片,我会得到它的标签值。如果我点击其他地方或背景,我会得到零。我一定会对这完全错误的。必须有一个更简单,更简单的方法来做到这一点。我在Visual Basic中进行了20多年的编程,他们在VB中做这样的事情太简单了。 –

0
//I'm not fan of this but this worked for me 
//you can create new imageView and pass tag of tapped subview 
//for example some imageView that is subview of a view 
//first: 
UIImageView *imageView; 
imageView.tag = 0; 

UITapGestureRecognizer *itemTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTap:)]; 

[imageView addGestureRecognizer:itemTap]; 
[someView addSubview:imageView]; 

//in the action selector imageTap: 
-(void)imageTap:(id)sender{ 
if ([(UIGestureRecognizer *)sender view].tag == 0) { 
UIImage *selectedImage; 
selectedImage.frame = [[(UIGestureRecognizer *)sender view] viewWithTag:0].frame; 
selectedImage.image = [UIImage imageNamed:@"newImage.png"]; 

//replace with the old: 
[someView addSubview:selectedImage]; 
}} 


GOODLUCK ;)