处理 Collection View Cell 中元素的手势事件
Handling gesture event on elements within Collection View Cell
如何处理在 Collection View Cell 内的不同元素内发生的手势事件(在我的例子中是点击)?
我正在使用 collection 视图,该视图是使用自定义单元构建的。此自定义单元格包含以下元素:
- 一个 UILabel
- 一个 UIImageView
- 一个按钮
如果用户点击 ImageView,则会向用户显示更大版本的图像。如果用户点击按钮,则图像将在数据库中删除。所以我需要一种方法来 a) 检测 collection 视图中哪个单元格被点击(类似于方法 didDeselectItemAtIndexPath 提供的功能和 b)区分元素可以在单元格内点击。
在点击 UIImageView 的情况下,我试图捕捉点击并确定哪个单元格是由 cellForItemAtIndexPath 方法中的以下代码完成的collection 视图:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
PostCellView *postCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PostCellView" forIndexPath:indexPath];
if(!postCell){
postCell = [[PostCellView alloc]init];
}
long row = indexPath.row;
PostModel *post = [self.newsfeedPosts objectAtIndex:row];
NSURL *posterUrl = [NSURL URLWithString:post.media[@"poster"]];
NSURLSessionDataTask *getPosterImage = [[NetworkHelper getInstance].session dataTaskWithURL:posterUrl completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *posterImage = [UIImage imageWithData:data];
postCell.posterImageView.image = posterImage;
//here is where I try to handle the tap event, if it happens on the uiimabeview
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(posterTapDetected:)];
[tapGestureRecognizer setDelegate:self];
[postCell.posterImageView addGestureRecognizer:tapGestureRecognizer];
});
}];
[getPosterImage resume];
return postCell;
}
假设您的集合视图是一个单独的部分,您可以向 UIImageView
添加一个标签来表示 UIImageView
的行和单元格,例如。
UIImage *posterImage = [UIImage imageWithData:data];
postCell.posterImageView.image = posterImage;
postCell.posterImageView.tag = row; // <-- add tag
然后识别 posterTapDetected:
中的行,例如:
- (void)posterTapDetected:(UITapGestureRecognizer*)gesture {
NSLog(@"row %d tapped", (int)gesture.view.tag);
}
至于区分可以在您的单元格中点击的不同元素,如果每个元素的手势识别器都链接到不同的方法,则识别点击的元素不需要任何必要的额外工作,因为每个不同元素的点击手势将由每个视图对应的 UITapGestureRecognizer
操作方法处理。
如何处理在 Collection View Cell 内的不同元素内发生的手势事件(在我的例子中是点击)?
我正在使用 collection 视图,该视图是使用自定义单元构建的。此自定义单元格包含以下元素:
- 一个 UILabel
- 一个 UIImageView
- 一个按钮
如果用户点击 ImageView,则会向用户显示更大版本的图像。如果用户点击按钮,则图像将在数据库中删除。所以我需要一种方法来 a) 检测 collection 视图中哪个单元格被点击(类似于方法 didDeselectItemAtIndexPath 提供的功能和 b)区分元素可以在单元格内点击。
在点击 UIImageView 的情况下,我试图捕捉点击并确定哪个单元格是由 cellForItemAtIndexPath 方法中的以下代码完成的collection 视图:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
PostCellView *postCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PostCellView" forIndexPath:indexPath];
if(!postCell){
postCell = [[PostCellView alloc]init];
}
long row = indexPath.row;
PostModel *post = [self.newsfeedPosts objectAtIndex:row];
NSURL *posterUrl = [NSURL URLWithString:post.media[@"poster"]];
NSURLSessionDataTask *getPosterImage = [[NetworkHelper getInstance].session dataTaskWithURL:posterUrl completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *posterImage = [UIImage imageWithData:data];
postCell.posterImageView.image = posterImage;
//here is where I try to handle the tap event, if it happens on the uiimabeview
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(posterTapDetected:)];
[tapGestureRecognizer setDelegate:self];
[postCell.posterImageView addGestureRecognizer:tapGestureRecognizer];
});
}];
[getPosterImage resume];
return postCell;
}
假设您的集合视图是一个单独的部分,您可以向 UIImageView
添加一个标签来表示 UIImageView
的行和单元格,例如。
UIImage *posterImage = [UIImage imageWithData:data];
postCell.posterImageView.image = posterImage;
postCell.posterImageView.tag = row; // <-- add tag
然后识别 posterTapDetected:
中的行,例如:
- (void)posterTapDetected:(UITapGestureRecognizer*)gesture {
NSLog(@"row %d tapped", (int)gesture.view.tag);
}
至于区分可以在您的单元格中点击的不同元素,如果每个元素的手势识别器都链接到不同的方法,则识别点击的元素不需要任何必要的额外工作,因为每个不同元素的点击手势将由每个视图对应的 UITapGestureRecognizer
操作方法处理。