确定点击了哪个子视图

时间:2013-10-21 15:08:26

标签: iphone ios objective-c uiview

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

将点按识别器添加到背景中(在View中加载):

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”,除非我点击最后添加的子视图。我需要知道的是访问抽头子视图TAG的正确格式,以便我可以将其设置为活动并移动它,或者有关如何执行此操作的任何其他建议。

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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];    

答案 2 :(得分: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 ;)