开发者

Detecting individual touches on Multiple UIImageviews?

i have added around 15 to 16 UIImageviews on my View using the following code

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

    }
}
}

now i know i can determine which imageview has been touched with the help of tag of imageView but i dont know how to check for the tags in touchesBegan method.. what can i possibly do to differentiate uiimageview on basis of touch??

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


UITouch *myTouch = [touc开发者_运维知识库hes anyObject];
CGPoint location = [myTouch locationInView:imgView];    

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

}

new code:

- (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];`

}


Please try this:

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


You can use UIGestureRecognizers as of iOS 3.2. I would recommend using one UIImageView for the menu you have and adding the UITapGestureRecognizer to detect single taps. Then use locationInView in the action to see where in the image the user touched.

    ...
    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
}

This is a link to the very helpful Apple guide to recognizers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜