2

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

在垂直滚动视图上添加前图像成功添加但不是随机打乱,即当我双击前图像时,显示后阵列图像但该图像视图中仍然存在前图像。

请帮我解决这个问题。

提前致谢。

请检查我的代码:

- (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 action:@selector(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)doubleTapImgView:(UITapGestureRecognizer *)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 action:@selector(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];

}
4

2 回答 2

1

为了实现这一点,您可以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]];
    }

子视图翻译:-

 [self.view bringSubviewToFront:frontview];
于 2013-05-15T05:25:15.417 回答
0

您可以为 backArray 和 front Array 中的图像使用相同的名称,或者您可以唯一地设置每个图像的标签值并比较这些标签来解决问题。

 imgV.tag=tagValue;

希望这可以帮助。

于 2013-05-15T05:30:37.817 回答