• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iphone - 获取图像索引

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:57:13 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在开发一个应用程序,其中我有 15 个图像存储在一个数组中作为正面图像,另外 15 个图像作为背面图像。我想将该图像垂直添加到 ScrollView 中,我已经成功完成了,但现在我的问题是如何比较这两个数组图像。

在垂直 ScrollView 上添加正面图像已成功添加但不是随机打乱,即当我双击正面图像时,会显示背面阵列图像,但该 ImageView 中仍存在正面图像。

请帮我解决这个问题。

提前致谢。

请检查我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];


    AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

    delegate.front=TRUE;
    delegate.back=FALSE;


    UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

    [scrollView setPagingEnabled:YES];

    [scrollView setShowsHorizontalScrollIndicator:NO];

    FrontsCards=[[NSMutableArray alloc]initWithObjects"cloub1.png",@"cloub2.png",@"cloub3.png",@"cloub4.png",@"cloub5.png",@"cloub6.png",@"cloub7.png",@"cloub8.png",@"cloub9.png",@"cloub10.png",@"cloub11.png",@"cloub12.png",@"diamond1.png",@"diamond2.png",@"diamond3.png",@"diamond4.png",@"diamond5.png", nil];

    for(int m=0; m<[FrontsCards count];m++)
      {
        ImgView.tag=m;

        int randIdx=arc4random()%[FrontsCards count];


        NSString *imageName=[FrontsCards objectAtIndex:randIdx];

    //  NSLog(@"%d",randIdx);


        NSString *fullImageName=[NSString stringWithFormat"%@",imageName];

        int padding=25;
        // padding is given.

        CGRect imageViewFrame=CGRectMake(scrollView.frame.size.width*m+padding, scrollView.frame.origin.y, scrollView.frame.size.width-2*padding, scrollView.frame.size.height);

        ImgView=[[UIImageView alloc]initWithFrame:imageViewFrame];

        [ImgView setImage:[UIImage imageNamed:fullImageName]];

        [ImgView setContentMode:UIViewContentModeScaleAspectFill];


        [scrollView addSubview:ImgView];


        UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self actionselector(doubleTapImgView];
        doubleTap.numberOfTapsRequired = 2;
        doubleTap.delegate = self;

        [self.ImgView addGestureRecognizer:doubleTap];

        self.ImgView.userInteractionEnabled=YES;


    }

    CGSize scrollViewSize=CGSizeMake(scrollView.frame.size.width*[FrontsCards count], scrollView.frame.size.height);
    [scrollView setContentSize:scrollViewSize];
    [self.view addSubview:scrollView];




}


- (void)doubleTapImgViewUITapGestureRecognizer *)gesture
{


    AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

    delegate.back=TRUE;

    delegate.front=FALSE;


    NSLog(@"%d", gesture.view.tag);

        NSLog(@"double-tap");

    BackCards =[[NSMutableArray alloc]initWithObjects"card1.jpg",@"card2.jpg",@"card3.jpg",@"card4.jpg", nil];

    UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

    [scrollView setPagingEnabled:YES];

    [scrollView setShowsHorizontalScrollIndicator:NO];

    for(int m=0; m< [BackCards count];m++)
    {       
        NSString *imageName=[BackCards objectAtIndex:m];

        NSString *fullImageName=[NSString stringWithFormat"%@",imageName];

        int padding=25;
        // padding is given.

        CGRect imageViewFrame=CGRectMake(scrollView.frame.size.width*m+padding, scrollView.frame.origin.y, scrollView.frame.size.width-2*padding, scrollView.frame.size.height);

        ImgView=[[UIImageView alloc]initWithFrame:imageViewFrame];

        [ImgView setImage:[UIImage imageNamed:fullImageName]];

        [ImgView setContentMode:UIViewContentModeScaleAspectFill];


        [scrollView addSubview:ImgView];


        UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self actionselector(doubleTapImgView];
        doubleTap.numberOfTapsRequired = 2;
        doubleTap.delegate = self;

        [self.ImgView addGestureRecognizer:doubleTap];

        self.ImgView.userInteractionEnabled=YES;


    }

    CGSize scrollViewSize=CGSizeMake(scrollView.frame.size.width*[FrontsCards count], scrollView.frame.size.height);
    [scrollView setContentSize:scrollViewSize];
    [self.view addSubview:scrollView];

}



Best Answer-推荐答案


为了实现这一点,您可以为每个图像设置 Tag 值,当您点击该特定图像时,您将获得标签值并使用该 tag 值。

self.imagview.tag=Unique_tagValue;

单击该图像时,您可以从另一个阵列设置图像。

self.imageview.image=[UIImage imageNamed:[backArray objectAtIndex:self.imageview.tag]];

编辑:-

viewDidLoad 方法中为 imageview 设置标签,像这样 ImgView.tag=m+100;

doubleTapImgView方法中保留以下代码

 for(int m=0; m< [BackCards count];m++)
    { 
      UIImageView *imgview=(UIImageView *)[scrollview viewWithTag:m+100];
      imgview.image=[UIImage imagNamed:[BackCards objectAtIndex:m]];
    }

subview 翻译:-

 [self.view bringSubviewToFront:frontview];

关于iphone - 获取图像索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16557466/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap