Ответ 1
Пользовательское взаимодействие UIImageView по умолчанию отключено .. Вы должны включить его явно, чтобы он реагировал на касания.
imageView.userInteractionEnabled = YES;
В настоящее время у меня есть пользовательский UITableViewCell, который содержит UIImageView и пытается добавить UITapGestureRecognizer в UIImageView без везения. здесь приведен фрагмент кода.
//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");
}
Я также установил userInteractionEnabled в ячейку и супервизор UIImageView, но все равно не повезло, никаких подсказок?
ИЗМЕНИТЬ:
Я также удаляю выбор ячейки по ячейке .selectionStyle = UITableViewCellSelectionStyleNone; Это может быть проблемой
Пользовательское взаимодействие UIImageView по умолчанию отключено .. Вы должны включить его явно, чтобы он реагировал на касания.
imageView.userInteractionEnabled = YES;
Swift 3
Это сработало для меня:
self.isUserInteractionEnabled = true
В моем случае это выглядит так:
- (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];
}