在 ipad 上水平显示 UIScrollview 中的图像并在点击时获取图像标签
Posted
技术标签:
【中文标题】在 ipad 上水平显示 UIScrollview 中的图像并在点击时获取图像标签【英文标题】:showing images in UIScrollview horizontally on ipad and getting tag of image on tap 【发布时间】:2013-10-28 07:33:09 【问题描述】:我正在使用 UIImageView 在 UIScrollView 中显示图像。我将 UIGestureRecognizer 添加到每个 ImageView,并且我正在设置 Image View 的标记属性。我想要在特定图像视图上点击图像视图的标记。但我只得到最后一个标记ImageView.Pasting Code 下面。您的帮助将不胜感激。
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];
并添加了获取手势事件的方法
- (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]]);
【问题讨论】:
【参考方案1】:你有很多 imageView 但只有一个 UITapGestureRecognizer。你应该为每个 imageView 添加一个手势识别器。
【讨论】:
如你所见,我在每个图像视图上都添加了手势识别器。为每个新的图像视图做新的手势识别器并不那么合乎逻辑 还有一点,NSTimeInterval是double类型,不能用view的tag来存储。 我写的代码工作正常,但我只是得到了最后一个 imageView 的点击手势事件,这不是预期的。 只需为每个图像视图添加一个新的点击手势,每个点击手势只能附加一个视图。【参考方案2】:在 for 循环中创建 UITapGestureRecognizer
。
for (NSDictionary *dict in object)
UITapGestureRecognizer *tapRecognizer;
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewDidTapped:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.delegate = self;
// your code for image imageview
[imageview addGestureRecognizer:tapRecognizer];
【讨论】:
以上是关于在 ipad 上水平显示 UIScrollview 中的图像并在点击时获取图像标签的主要内容,如果未能解决你的问题,请参考以下文章
iOS - 具有多个 UITableViews 的水平 UIScrollView