从多个图像视图中识别特定图像视图

发布于 2024-11-04 10:50:03 字数 1759 浏览 4 评论 0原文

我有一个图像视图,并在其上设置其他图像视图并将它们放在不同的位置。我想在法师视图上应用翻转动画。怎样才能做到呢?当 if(i==1) 时,我的图像视图被翻转。 img1 是我的图像视图,我想识别它。我在`imagearr 中分配了 52 个图像

我的代码:

-(void)setCards
{

    int k=0;
    CGFloat dx = self.view.frame.size.width/2;
    CGFloat dy = self.view.frame.size.height/2;

    int cnt = [imagearr count];


    for (int j=1; j<3; j++)            // Number of cards
    {
        for (int i=1; i<5; i++)         // Number of players
        {
            int rnd = (arc4random() % cnt)-1;   
            UIImage *img = [imagearr objectAtIndex:rnd];
            UIImageView *img1 = [[UIImageView alloc] initWithImage:img];

            CGRect frame = img1.frame;
            frame.size.width = frameX;
            frame.size.height = frameY;
            [img1 setFrame:frame];
            [img1 setTag:k+i];

            [UIView beginAnimations:@"Move" context:nil];
            [UIView setAnimationDuration:0.50];
            if (i==1)
            {
                [img1 setCenter:CGPointMake(dx+100, dy+(j*15))];

            }
            if (i==2) 
            {
                [img1 setCenter:CGPointMake(dx+(j*15), dy+60)];
            }
            if (i==3) 
            {
                [img1 setCenter:CGPointMake(dx-130, dy-(j*15))];
            }
            if (i==4) 
            {
                [img1 setCenter:CGPointMake(dx-(j*20), dy-95)];
            }

            [self.view addSubview:img1];
            [UIView commitAnimations];
            [img1 release];

        }
        k=k+10;      
    }


}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ 
    UITouch *touch = [[event allTouches] anyObject];
    int t = [touch view].tag;


}

我仍然无法识别特定的图像视图。请帮我解决这个问题。

I have a single image view, and I set other image views on it and put them in different locations. I want to apply FLIP ANIMATION on mage view. How can do it? when if(i==1) then my imageview is flipped. img1 is my image view and I want to identify it. I allocate 52 images in `imagearr

My code:

-(void)setCards
{

    int k=0;
    CGFloat dx = self.view.frame.size.width/2;
    CGFloat dy = self.view.frame.size.height/2;

    int cnt = [imagearr count];


    for (int j=1; j<3; j++)            // Number of cards
    {
        for (int i=1; i<5; i++)         // Number of players
        {
            int rnd = (arc4random() % cnt)-1;   
            UIImage *img = [imagearr objectAtIndex:rnd];
            UIImageView *img1 = [[UIImageView alloc] initWithImage:img];

            CGRect frame = img1.frame;
            frame.size.width = frameX;
            frame.size.height = frameY;
            [img1 setFrame:frame];
            [img1 setTag:k+i];

            [UIView beginAnimations:@"Move" context:nil];
            [UIView setAnimationDuration:0.50];
            if (i==1)
            {
                [img1 setCenter:CGPointMake(dx+100, dy+(j*15))];

            }
            if (i==2) 
            {
                [img1 setCenter:CGPointMake(dx+(j*15), dy+60)];
            }
            if (i==3) 
            {
                [img1 setCenter:CGPointMake(dx-130, dy-(j*15))];
            }
            if (i==4) 
            {
                [img1 setCenter:CGPointMake(dx-(j*20), dy-95)];
            }

            [self.view addSubview:img1];
            [UIView commitAnimations];
            [img1 release];

        }
        k=k+10;      
    }


}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ 
    UITouch *touch = [[event allTouches] anyObject];
    int t = [touch view].tag;


}

I still can't identify a specific imageview. Please help me to solve this problem.

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

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

发布评论

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

评论(7

乖不如嘢 2024-11-11 10:50:11

你可以尝试类似的东西

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event //here enable the touch       
{
    // get touch event      
    UITouch *touch = [[event allTouches] anyObject];
    int t = [touch view].tag;
    UIImageView *imageView = (UIImageView *) [self.view  viewWithTag:t];

    //your logic here
}

you could try something similar to this

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event //here enable the touch       
{
    // get touch event      
    UITouch *touch = [[event allTouches] anyObject];
    int t = [touch view].tag;
    UIImageView *imageView = (UIImageView *) [self.view  viewWithTag:t];

    //your logic here
}
海风掠过北极光 2024-11-11 10:50:10

您可以在图像视图上放置一个不可见的 UIbutton 并捕获触摸事件。效果是一样的,而且更干净、更简单。如果您需要更多信息,请告诉我。很乐意在这里编辑我的答案。

You can probably place a invisible UIbutton over your imageviews and capture touch event. The effect is the same and its much more cleaner and simpler. Let me know if you need more info. Would be happy to edit my answer here.

小傻瓜 2024-11-11 10:50:09

不同的 imageView 中会有不同的图像,因此每个 imageView 将位于不同的位置。

在您的触摸方法中,您是否必须检查触摸的边界,然后您必须匹配触摸区域下的 imageView。据此您可以继续您的功能。

There will be different images in different imageView so each imageView will be on different position.

in your touch method do you have to check the bounds of the touch and then you have to match which imageView lies under your touch region.According to that you can proceed your functionality.

喵星人汪星人 2024-11-11 10:50:08

将唯一的标签值与每个 UIImageView 实例相关联。

参考代码。

myImageView1.tag = 1;
myImageView1.tag = 2;
myImageView1.tag = 3;

Associate a unique tag values with each UIImageView instances.

Code for reference.

myImageView1.tag = 1;
myImageView1.tag = 2;
myImageView1.tag = 3;
在巴黎塔顶看东京樱花 2024-11-11 10:50:07

我认为你应该看一下Apple示例项目 GeekGameBoard< /a>.它实现了一个纸牌游戏,您将能够学习如何选择代表卡片的CALayers*,将它们拖动,并使它们彼此交互。


*从性能角度来看,这比使用视图更容易,而且可能更好。

I think you should take a look at the Apple sample project GeekGameBoard. It implements a solitaire game and you will be able to learn how to select CALayers* representing cards, drag them around, and have them interact with each other.


*Which will be both easier and probably better from a performance perspective than using views.

染柒℉ 2024-11-11 10:50:06

为单个imageView设置标签是最好的方法。使用标签,您甚至可以检测放置在表格视图单元格内的多个图像,并设置操作以选择单个标签。

Setting tag for individual imageView is the best approach. Using tag you could even detect multiples images placed inside tableview cells and set action for selection of individual tag as well.

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