Getting index in NSMutableArray of selected or tapped image

I am developing an app where I add images to an NSMutableArray and display them in an image view.

My problem is that I will don't know how to get the index of the selected or tapped image in my app.

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];

What should I do in my tap gesture recognizer to get the index of the image?

- (void)doubleTapImgView:(UITapGestureRecognizer *)gesture
{
     NSLog(@"double-tap");
}

Answers


maybe you can use something like this:

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

then in the tapRecognizer:

-(void)doubleTapImgView:(UITapGestureRecognizer *)gesture{
    UIImageView *imgView = (UIImageView *)gesture.view;
     int idx = [FrontCards indexOfObject:[imgView accessibilityIdentifier]];
}

set a tag to each imageView you using like this :

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

and then replace this method:

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

it will print you the index of the image in the imageView


Need Your Help


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.