Iphone 正在获取选定或点击图像的NSMutableArray中的索引
我正在开发一个应用程序,将图像添加到Iphone 正在获取选定或点击图像的NSMutableArray中的索引,iphone,ios,objective-c,Iphone,Ios,Objective C,我正在开发一个应用程序,将图像添加到NSMutableArray,并在图像视图中显示它们 我的问题是,我不知道如何在我的应用程序中获取所选或点击图像的索引 FrontsCards=[[NSMutableArray alloc]initWithObjects:@"cloub1.png",@"cloub2.png",@"cloub3.png",@"cloub4.png", nil]; for(int m=0; m< [FrontsCards count];m++) { NSStrin
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];
为使用的每个imageView设置一个标记,如下所示:
ImgView=[[UIImageView alloc]initWithFrame:imageViewFrame];
ImgView.tag = m;
然后替换此方法:
- (void)doubleTapImgView:(UITapGestureRecognizer *)gesture
{
NSLog(@"double-tap");
NSLog(@"%d", gesture.view.tag);
}
它将在图像视图中打印图像的索引也许您可以使用以下方法:
first add the name of the image as the accessibilityIdentifier of the the imageview
[imgView setAccessibilityIdentifier:imageName];
然后在Tap识别器中:
-(void)doubleTapImgView:(UITapGestureRecognizer *)gesture{
UIImageView *imgView = (UIImageView *)gesture.view;
int idx = [FrontCards indexOfObject:[imgView accessibilityIdentifier]];
}
如果您想支持我,请帮助我提高mu的声誉。在这种情况下,signature.view将提供ImageView,并且在FrontCards阵列中包含UIImage。所以它不会给你索引。只想让你知道:)@Ushan87谢谢!更新了我的代码,也许现在可以用了。我使用了AccessibilityIdentifier:)
-(void)doubleTapImgView:(UITapGestureRecognizer *)gesture{
UIImageView *imgView = (UIImageView *)gesture.view;
int idx = [FrontCards indexOfObject:[imgView accessibilityIdentifier]];
}