-1

我正在开发一个应用程序,我在其中添加图像NSMutableArray并在图像视图中显示它们。

我的问题是我不知道如何在我的应用程序中获取所选或点击的图像的索引。

FrontsCards=[[NSMutableArray alloc]initWithObjects:@"cloub1.png",@"cloub2.png",@"cloub3.png",@"cloub4.png", nil];

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

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

    int padding=25;

    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
{
     NSLog(@"double-tap");
}
4

2 回答 2

1

为您使用的每个 imageView 设置一个标签,如下所示:

ImgView=[[UIImageView alloc]initWithFrame:imageViewFrame];
ImgView.tag = m;

然后替换此方法:

- (void)doubleTapImgView:(UITapGestureRecognizer *)gesture
{
     NSLog(@"double-tap");
     NSLog(@"%d", gesture.view.tag);
}

它会打印你在imageView中的图像索引

于 2013-05-14T08:30:31.840 回答
0

也许你可以使用这样的东西:

first add the name of the image as the accessibilityIdentifier of the the imageview
[imgView setAccessibilityIdentifier:imageName];

然后在tapRecognizer中:

-(void)doubleTapImgView:(UITapGestureRecognizer *)gesture{
    UIImageView *imgView = (UIImageView *)gesture.view;
     int idx = [FrontCards indexOfObject:[imgView accessibilityIdentifier]];
}
于 2013-05-14T08:42:48.060 回答