在 ipad 上水平显示 UIScrollview 中的图像并在点击时获取图像标签 [英] showing images in UIScrollview horizontally on ipad and getting tag of image on tap

查看:22
本文介绍了在 ipad 上水平显示 UIScrollview 中的图像并在点击时获取图像标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在带有 UIImageView 的 UIScrollView 中显示图像.我向每个 ImageView 添加了 UIGestureRecognizer 并且我正在设置图像视图的标签属性.我想要在特定图像视图上点击图像视图的标签.但我只得到最后的标签ImageView.Pasting 代码如下.您的帮助将不胜感激.

I am showing images in UIScrollView with UIImageView.I added UIGestureRecognizer to each ImageView and also I am setting tag property of Image View.I want tag of Image view on Tap on particular Image View.But I am only getting Tag of last ImageView.Pasting Code below.Your help will be appreciated.

UITapGestureRecognizer *tapRecognizer;
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewDidTapped:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.delegate = self;
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(5, 60, self.view.bounds.size.width-20, self.view.bounds.size.height-100)];

scrollView.delegate = self;
scrollView.pagingEnabled = YES;
scrollView.scrollEnabled = YES;
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
[scrollView setCanCancelContentTouches:YES];
[scrollView setUserInteractionEnabled:YES];

float width;
for (NSDictionary *dict in object) {
UIImageView *backgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(width, 0,      self.view.bounds.size.width-20, self.view.bounds.size.height-40)];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
[dateformatter setDateFormat:@"yyyy-MM-dd"];
[dateformatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [dateformatter  dateFromString:[NSString stringWithFormat:@"%@",[dict objectForKey:@"id"]]];
NSTimeInterval timeStamp = [date timeIntervalSince1970];
backgroundImageView.tag = timeStamp;
[backgroundImageView setUserInteractionEnabled:YES];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[dict objectForKey:@"coverImage"]]];
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request
imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
 backgroundImageView.image = image;
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
 NSLog(@"Error %@",error);
}];
[operation start];
width = width + backgroundImageView.frame.size.width;

[backgroundImageView addGestureRecognizer:tapRecognizer];
[imageViewArray addObject:backgroundImageView];
[scrollView addSubview:backgroundImageView];
}

并添加了获取手势事件的方法

And added method for getting gesture event

- (void)imageViewDidTapped:(UIGestureRecognizer *)gesture{
UITapGestureRecognizer *tapGesture = (UITapGestureRecognizer *)gesture;

UIImageView *tappedImageView = (UIImageView *)[tapGesture view];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
[dateformatter setDateFormat:@"yyyy-MM-dd"];
[dateformatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(@"Issue Date :%@",[dateformatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:tappedImageView.tag]]);

 }

推荐答案

你有很多 imageView 但只有一个 UITapGestureRecognizer.您应该为每个 imageView 添加一个手势识别器.

you have many imageView but only one UITapGestureRecognizer. you should add a gesture recognizer for each imageView.

这篇关于在 ipad 上水平显示 UIScrollview 中的图像并在点击时获取图像标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆