У меня есть пользовательская ячейка с различными IBOutlets, но на одной кнопке я хочу добавить UILongPressGestureRecognizer для длинных жестов в прессе. Вот мой код (кстати выходы подключены правильно и IBAction
метод кнопки называется правильно):Добавление UILongPressGestureRecognizer в UIButton внутри пользовательского UITableViewCell
MyCustomCell.h
@interface MyCustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UIButton *myButton;
@property (strong, nonatomic) UILongPressGestureRecognizer *longPressGestureRecognizer;
@end
MyCustomCell.m
- (void)awakeFromNib
{
// Initialization code
self.longPressGestureRecognizer = nil;
}
MyViewController.m
#import MyCustomCell.h
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"MyCell";
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
if (!cell){
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGestures:)];
cell.longPressGestureRecognizer.minimumPressDuration = 1.0f;
cell.longPressGestureRecognizer.allowableMovement = 300.0f;
[cell.myButton addGestureRecognizer:cell.longPressGestureRecognizer];
}
- (void)handleLongPressGestures:(UIGestureRecognizer *)recognizer
{
if ([recognizer.view isKindOfClass:[UIButton class]]){
if (recognizer.state == UIGestureRecognizerStateBegan){
NSLog(@"Long press began");
} else if (recognizer.state = UIGestureRecognizerStateEnded){
NSLog(@"Long press ended");
}
}
}
Проблема заключается в том handleLongPressGestures:
метод никогда не вызывается.
Я также попытался использовать 'longPressGestureRecognizer' как свойство в контроллере представления, но не работал. – flizana