UITableViewCell 背景选择状态在手势结束前翻转
代码
我有一些代码将名为 _recognizer
的 UILongPressGestureRecognizer
手势识别器添加到名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将测试条件从
UIGestureRecognizerStateEnded
更改为UIGestureRecognizerStateBegan
,并且手势与单元格选择状态更改同步:以这种方式命名事件似乎违反直觉,但这似乎有效。
I changed my test condition from
UIGestureRecognizerStateEnded
toUIGestureRecognizerStateBegan
and the gesture is timed with the cell selection state change:Seems counterintuitive naming the event this way, but that seems to work.