我目前有一个自定义的UITableViewCell,其中包含一个UIImageView,并试图在UIImageView上添加UITapGestureRecognizer而没有运气。这是代码片段。
//within cellForRowAtIndexPath (where customer table cell with imageview is created and reused)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageTap:)];
tap.cancelsTouchesInView = YES;
tap.numberOfTapsRequired = 1;
tap.delegate = self;
[imageView addGestureRecognizer:tap];
[tap release];
// handle method
- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer {
RKLogDebug(@"imaged tab");
}
我还在单元格和UIImageView的超级视图上设置了userInteractionEnabled,但仍然没有运气,任何提示?
编辑:
我还通过cell.selectionStyle = UITableViewCellSelectionStyleNone删除了单元格的选择;这可能是个问题吗
答案 0 :(得分:102)
UIImageView的用户互动已停用。您必须明确启用它才能使其响应触摸。
imageView.userInteractionEnabled = YES;
答案 1 :(得分:3)
Swift 3
这对我有用:
self.isUserInteractionEnabled = true
答案 2 :(得分:2)
就我而言,它看起来像:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = CELL_ROUTE_IDENTIFIER;
RouteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[RouteTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
if ([self.routes count] > 0) {
Route *route = [self.routes objectAtIndex:indexPath.row];
UITapGestureRecognizer *singleTapOwner = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(imageOwnerTapped:)];
singleTapOwner.numberOfTapsRequired = 1;
singleTapOwner.cancelsTouchesInView = YES;
[cell.ownerImageView setUserInteractionEnabled:YES];
[cell.ownerImageView addGestureRecognizer:singleTapOwner];
} else {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
选择器:
- (void)imageOwnerTapped:(UISwipeGestureRecognizer *)gesture {
CGPoint location = [gesture locationInView:self.tableView];
NSIndexPath *tapedIndexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *tapedCell = [self.tableView cellForRowAtIndexPath:tapedIndexPath];
NSIndexPath *indexPath = [self.tableView indexPathForCell:tapedCell];
NSUInteger index = [indexPath row];
Route *route = [self.routes objectAtIndex:index];
}