Ios 将UILongPressGestureRecognitor添加到自定义UITableViewCell内的UIButton
我有一个带有各种IBoutlet的自定义单元格,但我想在一个按钮上添加一个UILongPressGestureRecognitor,用于长按手势。这是我的代码(顺便说一句,插座连接正确,按钮的Ios 将UILongPressGestureRecognitor添加到自定义UITableViewCell内的UIButton,ios,objective-c,uitableview,uilongpressgesturerecogni,Ios,Objective C,Uitableview,Uilongpressgesturerecogni,我有一个带有各种IBoutlet的自定义单元格,但我想在一个按钮上添加一个UILongPressGestureRecognitor,用于长按手势。这是我的代码(顺便说一句,插座连接正确,按钮的iAction方法调用正确): 问题是handleLongPress手势:方法从未调用。长按手势识别器应是控制器上的属性,而不是视图(MyCustomCell)。将属性移到MyViewController上,然后重试。我猜当它在MyCustomCell中排队和退队时,会发生一些奇怪的事情 重用的对象(单元)
iAction
方法调用正确):
问题是
handleLongPress手势:
方法从未调用。长按手势识别器应是控制器上的属性,而不是视图(MyCustomCell)。将属性移到MyViewController上,然后重试。我猜当它在MyCustomCell中排队和退队时,会发生一些奇怪的事情重用的对象(单元)应该是轻量级的。在这种情况下,LongPressGestureRecognitor的目标是视图控制器,非常讨厌。试试这个
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILongPressGestureRecognizer *LongPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGestures:)];
LongPress.minimumPressDuration = 1.0f;
LongPress.allowableMovement = 300.0f;
[cell.myButton addGestureRecognizer:LongPress];
}
- (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");
}
}
}
这样试试看
- (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];
}
if ([[cell gestureRecognizers] count]<1) {
UILongPressGestureRecognizer *longPressGestureRecognizer;
longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGestures:)];
longPressGestureRecognizer.minimumPressDuration = 1.0f;
longPressGestureRecognizer.allowableMovement = 300.0f;
longPressGestureRecognizer.delegate = self;
[cell.myButton addGestureRecognizer:cell.longPressGestureRecognizer];
}
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*标识符=@“MyCell”;
MyCustomCell*cell=[tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
如果(!单元格){
cell=[[MyCustomCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:标识符];
}
如果([[cell gestureRecognizers]count]我也尝试将LongPress GestureRecognizer
用作视图控制器中的属性,但没有成功。
- (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];
}
if ([[cell gestureRecognizers] count]<1) {
UILongPressGestureRecognizer *longPressGestureRecognizer;
longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGestures:)];
longPressGestureRecognizer.minimumPressDuration = 1.0f;
longPressGestureRecognizer.allowableMovement = 300.0f;
longPressGestureRecognizer.delegate = self;
[cell.myButton addGestureRecognizer:cell.longPressGestureRecognizer];
}
}