UITableViewCell 背景选择状态在手势结束前翻转

发布于 2024-09-10 06:38:45 字数 1340 浏览 5 评论 0原文

代码

我有一些代码将名为 _recognizerUILongPressGestureRecognizer 手势识别器添加到名为 UITableViewCell 的子类中>cell:

...
UILongPressGestureRecognizer *_recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(cellLongPressRecognized:)];
_recognizer.allowableMovement = 20;
_recognizer.minimumPressDuration = 1.0f;
[[cell contentView] addGestureRecognizer:_recognizer];
[_recognizer release];
...

-cellLongPressRecognized: 选择器仅在手势结束时进行记录:

- (void) cellLongPressRecognized:(id)_sender {
    if (((UILongPressGestureRecognizer *)_sender).state == UIGestureRecognizerStateEnded)
        ALog(@"[MyViewController] -cellLongPressRecognized: gesture ended...");
}

当我点击、按住并释放单元格时,我的控制台会显示一条日志消息:

[MyViewController] -cellLongPressRecognized: gesture ended...

到目前为止,一切顺利。

问题

问题是表格单元格的背景仅保持选中状态 1.0 秒,即 _recognizer.minimumPressDuration 属性。

如果我将手指放在设备上的时间超过 1.0 秒,单元格的背景将从 UITableViewCellSelectionStyleBlue 选择样式翻转回其通常的、不透明的、未选定的背景。

为了确保此问题仅涉及特定于手势的代码,我在测试时禁用了 -tableView:didSelectRowAtIndexPath:

问题

如何无限期地保持背景选定,仅在“长按”手势结束时才翻转回来?

CODE

I have some code that adds a UILongPressGestureRecognizer gesture recognizer called _recognizer to a subclass of a UITableViewCell called cell:

...
UILongPressGestureRecognizer *_recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(cellLongPressRecognized:)];
_recognizer.allowableMovement = 20;
_recognizer.minimumPressDuration = 1.0f;
[[cell contentView] addGestureRecognizer:_recognizer];
[_recognizer release];
...

The -cellLongPressRecognized: selector simply logs when the gesture ends:

- (void) cellLongPressRecognized:(id)_sender {
    if (((UILongPressGestureRecognizer *)_sender).state == UIGestureRecognizerStateEnded)
        ALog(@"[MyViewController] -cellLongPressRecognized: gesture ended...");
}

My console shows one log message when I tap, hold and release a cell:

[MyViewController] -cellLongPressRecognized: gesture ended...

So far, so good.

ISSUE

The issue is that the table cell's background stays selected only as long as 1.0 second, the _recognizer.minimumPressDuration property.

If I hold my finger on the device any longer than 1.0 second, the cell's background flips back from the UITableViewCellSelectionStyleBlue selection style to its usual, opaque, non-selected background.

To make sure only gesture-specific code is involved with this issue, I have disabled -tableView:didSelectRowAtIndexPath: while testing.

QUESTION

How do I keep the background selected indefinitely, flipped back only when the "long press" gesture ends?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羞稚 2024-09-17 06:38:45

我将测试条件从 UIGestureRecognizerStateEnded 更改为 UIGestureRecognizerStateBegan,并且手势与单元格选择状态更改同步:以

- (void) cellLongPressRecognized:(id)_sender {
    if (((UILongPressGestureRecognizer *)_sender).state == UIGestureRecognizerStateBegan)
        ALog(@"[MyViewController] -cellLongPressRecognized: gesture began...");
}

这种方式命名事件似乎违反直觉,但这似乎有效。

I changed my test condition from UIGestureRecognizerStateEnded to UIGestureRecognizerStateBegan and the gesture is timed with the cell selection state change:

- (void) cellLongPressRecognized:(id)_sender {
    if (((UILongPressGestureRecognizer *)_sender).state == UIGestureRecognizerStateBegan)
        ALog(@"[MyViewController] -cellLongPressRecognized: gesture began...");
}

Seems counterintuitive naming the event this way, but that seems to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文