iOS - Delayed quot;Touch Downquot; event for UIButton in UITableViewCell(iOS - 延迟的“Touch DownUITableViewCell 中的 UIButton 事件)
问题描述
我有一个自定义的 UITableViewCell,它由以下内容初始化:
I have a custom UITableViewCell, which is initialized by the following:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
self = [nibArray objectAtIndex:0];
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
[self.downButton setBackgroundImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal];
[self.downButton setBackgroundImage:[UIImage imageNamed:@"buttonSelected"] forState:UIControlStateHighlighted];
}
return self;
}
按钮正确显示,带有适当的背景图像,但在按下/单击按钮时,突出显示的图像不会立即出现.相反,您必须在更改发生之前按住它一两秒钟.另一方面,释放按钮确实会立即变回原始背景图像.
The button appears properly, with the appropriate background image, but the highlighted image does not instantly appear when the button is pressed/clicked. Instead, you have to hold it down for a second or two before the change occurs. Releasing the button, on the other hand, does have an instant change back to the original background image.
为了减轻切换到突出显示的图像时的延迟变化,我将更改放在以下方法中:
Trying to mitigate the tardy change when switching to the highlighted image, I put the change in the following method:
- (IBAction)downDown:(id)sender {
[self.downButton setBackgroundColor:[UIColor redColor]];
}
上面的方法设置为Touch Down"(相对于更常见的Touch Up Inside"),并且我已经删除了突出显示状态的 setBackgroundImage:forState:
.和上面提到的一样的问题.颜色最终会变为红色,但仅在单击并按住按钮一两秒后才会发生.
The method above is set for "Touch Down" (opposed to the more common "Touch Up Inside"), and I have removed the setBackgroundImage:forState:
for the highlighted state. Same problem as mentioned above. The color does eventually change to red, but only after clicking and holding on the button for a second or two.
我有一个在Touch Up Inside"发生时调用的按钮方法,并且该方法执行没有问题 - 无论我是快速点击按钮,还是在释放之前单击并按住它一段时间.
I have a method for the button that is called when "Touch Up Inside" occurs, and that method executes without issue - regardless of whether I quickly tap the button, or click and hold on it for a length of time before releasing.
那么为什么会延迟Touch Down"或UIControlStateHighlighted
?我正在尝试向用户提供即时反馈,以表明该按钮已被按下.
So why the delay for the "Touch Down" or UIControlStateHighlighted
? I'm trying to provide instant feedback to the user to show that the button has been pressed.
如果需要,我可以提供更多代码,但这些是唯一与背景外观有关的部分.
I can provide more code if needed, but these are the only bits that have anything to do with the background appearance.
推荐答案
这是由 UIScrollView
属性 delaysContentTouches
引起的.
This is caused by the UIScrollView
property delaysContentTouches
.
对于 UITableView
本身,只需将该属性设置为 NO
就足够了,但这仅适用于未包含在另一个表中的子视图UIScrollView
.
It used to be sufficient to just set that property to NO
for the UITableView
itself, but that will only work for subviews of the table that are not encased in another UIScrollView
.
UITableViewCells
在 iOS 7 中包含一个内部滚动视图,因此您需要在单元格级别为所有带有按钮的单元格更改此属性的值.
UITableViewCells
contain an internal scroll view in iOS 7 so you will need to change the value of this property on the cell level for all cells with buttons in them.
这是你需要做的:
1.在 viewDidLoad
或类似的地方,一旦你的 UITableView
被初始化,把它放进去:
1.in viewDidLoad
or somewhere similar once your UITableView
has been initialized, put this in:
self.tableView.delaysContentTouches = NO;
2.对于 iOS 7 支持,在 UITableViewCell
的初始化方法中(initWithStyle:reuseIdentifier:
或 initWithCoder:
用于 NIB),把这个放在最后:
2.for iOS 7 support, in the initialization method for your UITableViewCell
(initWithStyle:reuseIdentifier:
or initWithCoder:
for NIBs), put this in at the end:
for (UIView *currentView in self.subviews)
{
if([currentView isKindOfClass:[UIScrollView class]])
{
((UIScrollView *)currentView).delaysContentTouches = NO;
break;
}
}
不幸的是,这不是一个 100% 永久的解决方案,因为 Apple 可以在未来再次更改单元格内的视图层次结构(可能将滚动视图向下移动另一层或需要您在其中嵌套另一个循环的东西),但直到他们以某种方式向开发人员展示了类或至少是属性,这是我们所拥有的最好的.
This is unfortunately not a 100% permanent solution as Apple can change the view hierarchy inside cells again in the future (perhaps moving the scroll view another layer down or something which would require you to nest another loop in there), but until they surface the class or at least the property to developers somehow, this is the best we've got.
这篇关于iOS - 延迟的“Touch Down"UITableViewCell 中的 UIButton 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iOS - 延迟的“Touch Down"UITableViewCell 中的 UIButton 事件
基础教程推荐
- 更改 UITableView 部分标题的颜色 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 在视图控制器的宽度上水平均匀分布 UIButton 的最简单方法? 2022-01-01
- 在 iOS 7 下 CCMenu 错位 2022-01-01
- UINavigationItem 的持久 rightBarButtonItem 属性 2022-01-01
- UINavigationBar 隐藏按钮文本 2022-01-01
- EditText 中的 setHintTextColor() 2022-01-01
- Android - 如何在runOnUiThread 中将数据传递给Runnable? 2022-01-01
- Firebase 云消息传递令牌未生成 2022-01-01
- 从 UIWebView 访问元数据 2022-01-01