gpt4 book ai didi

ios - 将 UILongPressGestureRecognizer 添加到自定义 UITableViewCell 中的 UIButton

转载 作者:行者123 更新时间:2023-11-29 12:21:03 24 4
gpt4 key购买 nike

我有一个带有各种 IBOutlets 的自定义单元格,但我想在一个按钮上添加一个 UILongPressGestureRecognizer 用于长按手势。这是我的代码(顺便说一句, socket 连接正确,按钮的 IBAction 方法被正确调用):

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 应该是 Controller 的属性,而不是 View (MyCustomCell)。将属性移至 MyViewController 并重试。我的猜测是当它对 MyCustomCell 进行排队和出队时发生了一些奇怪的事情。

重用的对象(单元)应该是轻量级的。在这种情况下,longPressGestureRecognizer 的目标是 View Controller 并且很讨厌。

关于ios - 将 UILongPressGestureRecognizer 添加到自定义 UITableViewCell 中的 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30626888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com