1

UIImageView を使用して UIScrollView に画像を表示しています。各 ImageView に UIGestureRecognizer を追加しました。また、Image View のタグ プロパティを設定しています。以下のコード。ご協力をお願いいたします。

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

 }
4

2 に答える 2

1

多くのimageViewがありますが、UITapGestureRecognizerは1つだけです。imageView ごとにジェスチャ認識エンジンを追加する必要があります。

于 2013-10-28T07:49:48.153 に答える
0

UITapGestureRecognizerfor ループ内に作成します。

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];
}
于 2013-10-28T09:59:55.367 に答える