检测多个 UIImageview 上的单独触摸?

发布于 2024-11-07 03:24:12 字数 2899 浏览 2 评论 0原文

我使用以下代码在我的视图上添加了大约 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 已被触摸,但我不知道如何检查 TouchBegan 方法中的标签..可以做什么我可能会根据触摸来区分 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];`

}

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 = [touches 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];`

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七颜 2024-11-14 03:24:12

请尝试这个:

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

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);
}
陌生 2024-11-14 03:24:12

从 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 识别器指南

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文