检测多个UIImageviews上的个别触摸?

时间:2011-05-11 08:01:31

标签: ios ios4 uiimageview

我使用以下代码在我的视图中添加了大约15到16个UIImageviews

- (void) setUpCellsUsingImage: (UIImage *) masterImage
{
  rows = 4;
 cols = 4;
 containerCellHeight=hight/4;
containerCellWidth=width/4; 


NSInteger row, col;
CGImageRef tempSubImage;
CGRect tempRect;
CGFloat yPos, xPos; 

UIImage * aUIImage;
UIImageView *label;

cellArray = [[NSMutableArray new] autorelease];
int i =0;

for (row=0; row < rows; row++) {
    yPos = row * containerCellHeight;
    for (col=0; col < cols; col++) {
        xPos = col * containerCellWidth;
        label = [[UIImageView alloc]init];
        tempRect = CGRectMake(xPos, yPos, containerCellWidth, containerCellHeight);     

        tempSubImage = CGImageCreateWithImageInRect(masterImage.CGImage, tempRect);

        aUIImage = [UIImage imageWithCGImage: tempSubImage];

        imgView = [[UIImageView alloc] initWithImage:aUIImage];

        imgView.tag =i;

        i++;

        NSLog(@"original tags = %d",label.tag);

        [cellArray addObject: aUIImage];        

        aUIImage = nil;
        CGImageRelease(tempSubImage);

    }
}
}

现在我知道我可以在imageView的标签的帮助下确定触摸了哪个imageview但我不知道如何在touchesBegan方法中检查标签..我可以做些什么来区分uiimageview的触摸?

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {


UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:imgView];    

NSLog(@"touch  %@",imgView.tag);

}

新代码:

- (void) setUpCellsUsingImage: (UIImage *) masterImage
{

  rows = 4;
 cols = 4;
 containerCellHeight=hight/4;
containerCellWidth=width/4; 

NSInteger row, col;
CGImageRef tempSubImage;
CGRect tempRect;
CGFloat yPos, xPos; 

UIImage * aUIImage;
UIImageView *label;

cellArray = [[NSMutableArray new] autorelease];
int i =0;

for (row=0; row < rows; row++) {
    yPos = row * containerCellHeight;
    for (col=0; col < cols; col++) {
        xPos = col * containerCellWidth;
        label = [[UIImageView alloc]init];
        tempRect = CGRectMake(xPos, yPos, containerCellWidth, containerCellHeight);     

        tempSubImage = CGImageCreateWithImageInRect(masterImage.CGImage, tempRect);

        aUIImage = [UIImage imageWithCGImage: tempSubImage];

        imgView = [[UIImageView alloc] initWithImage:aUIImage];

        [self.view addSubview:imgView]; // i add the uiimageview here

        imgView =CGRectMake(xPos, yPos, containerCellWidth-1, containerCellHeight-1);

        imgView.tag =i;

        i++;

        NSLog(@"original tags = %d",label.tag);

        [cellArray addObject: aUIImage];        

        aUIImage = nil;
        CGImageRelease(tempSubImage);

    }
}
}



- (void)viewDidLoad {

    UIImage *mImage = [UIImage imageNamed:@"menu.png"];
     hight = mImage.size.height;
     width = mImage.size.width;

    [self setUpCellsUsingImage:mImage];

[`super viewDidLoad];`

}

2 个答案:

答案 0 :(得分:1)

请试试这个:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:imgView];    

    UIView *hitView = [self.view hitTest:location withEvent:event];
    NSLog(@"hitView %@",hitView);

    UIImageView *hitImageView = nil;

    if([hitView isKindOfClass:[UIImageView class]]) {
        hitImageView = (UIImageView *)hitImageView;
    } 

    NSLog(@"touched %@", hitImageView);
}

答案 1 :(得分:1)

从iOS 3.2开始,您可以使用UIGestureRecognizers。我建议您使用一个UIImageView作为菜单,并添加UITapGestureRecognizer来检测单个点击。然后在操作中使用locationInView查看用户触摸的图像中的位置。

    ...
    UITapGestureRecognizer *tapRecognizer = 
        [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(menuSelect:)];
    [imageView addGestureRecognizer:tapRecognizer];
    [tapRecognizer release]; 
}

- (void)menuSelect:(UIGestureRecognizer *)gesture {
    // get location CGPoint for touch from recognizer
}

这是指向非常有用的Apple guide to recognizers的链接。