0

我有一个背景图像(imageView - 标记 0),它已由几个标记图像子视图(imgView - 标记 1 - ?)填充。我在主背景图像中添加了一个点击识别器,并且还尝试了子视图。我可以通过点击进入后台点击识别程序,但无法识别已点击哪个子视图。

将点击识别器添加到背景(在 View did Load 期间):

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); 
}

这正确地给了我子视图的数量,但除非我点击最后添加的子视图,否则总是返回“TAG:0”。我需要知道的是访问被点击子视图的标签的正确格式,以便我可以使其处于活动状态并移动它,或者关于如何执行此操作的任何其他建议。

4

3 回答 3

0

将断点/NSLog 的标记值放在imgView. 代码片段不会告诉您tag变量的范围(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];    
于 2013-10-21T15:54:06.620 回答
0

我不确定要这么说,但我想关键是你的tag变量没有改变。如果tag变量不是整个“.m”文件的全局变量,则无法将其值保存在不同的函数或方法中。尝试在“.h”文件中声明它并给它一些其他名称(例如 Tag 或 tagValue),以便编译器可以改变它应该返回的内容(变量的标签或你的变量),然后添加self.Tagself.tagValue(无论你命名它)每次你将它分配给一个int。祝你好运,让我知道它是否有效。

于 2013-10-21T15:19:06.277 回答
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 ;)
于 2015-08-05T12:21:42.183 回答