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

查看:145
本文介绍了iOS - 延迟“触及” UITutton在UITableViewCell中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的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 property delaysContentTouches

This is caused by the UIScrollView property delaysContentTouches.

过去只需将该属性设置为 用于 UITableView 本身,但这仅适用于未包含在另一个 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.

以下是您需要执行的操作:

Here is what you need to do:

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 <的初始化方法中/ code>(对于NIB, initWithStyle:reuseIdentifier: initWithCoder:),最后把它放进去:

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;
    }
}

遗憾的是,这不是Apple的100%永久性解决方案将来可以再次更改单元格内的视图层次结构(可能将滚动视图向下移动另一层或者需要在其中嵌套另一个循环的东西),但是直到它们以某种方式将类或至少属性显示给开发人员,是我们得到的最好的。

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

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