iOS - 延迟“触地"UITableViewCell 中 UIButton 的事件 [英] iOS - Delayed "Touch Down" event for UIButton in UITableViewCell

查看:21
本文介绍了iOS - 延迟“触地"UITableViewCell 中 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: or initWithCoder: for NIBs),把这个放在最后:

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 - 延迟“触地"UITableViewCell 中 UIButton 的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆