简体   繁体   English

在tableview滚动时拦截UITableView中的TouchUpInside事件

[英]Intercept TouchUpInside event in UITableView while tableview scrolls

I'm building out a UITableView that has a button in the tableview's section header, so that as the user scrolls down the screen, the button is pinned to the top of the screen. 我正在构建一个UITableView,它在tableview的节头中有一个按钮,这样当用户向下滚动屏幕时,按钮就会固定在屏幕的顶部。

The problem is that the touch events for the section header's button don't register when the UITableView is moving . 问题是当UITableView移动时,节标题按钮触摸事件不会注册 The button only gets the touch event if the tableview is not scrolling. 如果tableview不滚动,该按钮仅获取触摸事件。

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (!_sectionHeaderView) {
        _sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];

        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        backButton.frame = CGRectMake(0, 0, 50, 50);
        [backButton setImage:[UIImage imageNamed:@"upArrow"] forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(scrollToTop:) forControlEvents:UIControlEventTouchUpInside];
        [_sectionHeaderView addSubview:backButton];

    }
    return _sectionHeaderView;
}

Currently, what happens is: - If the tableview is not scrolling, the button performs as you'd expect: tap it, and the button gets the TouchUpInside gesture. 目前,会发生以下情况: - 如果桌面视图没有滚动,则按钮按预期执行:点击它,按钮将获得TouchUpInside手势。 - If the tableview is scrolling, the button doesn't get the TouchUpInside gesture - instead, the tableview just stops in place. - 如果桌面视图正在滚动,则该按钮不会获​​得TouchUpInside手势 - 相反,桌面视图就会停止。

I've tried subclassing the UITableView and looking at the touchesBegan: methods, etc and that didn't work. 我已经尝试了子类化UITableView并查看touchesBegan:方法等,但是没有用。 I see the same pattern: the gestures only show up if/when the tableview isn't moving. 我看到相同的模式:只有/当tableview没有移动时,手势才会出现。

Update: This isn't a usability issue - while I can't show screenshots of the design I'm working on, it's a valid use case to use buttons & controls in the section header. 更新:这不是一个可用性问题 - 虽然我无法显示我正在处理的设计的屏幕截图,但是在节标题中使用按钮和控件是一个有效的用例。

Here's a quick sketch to explain why: 这是一个快速草图来解释原因:

浮动节头

Figured it out! 弄清楚了! All you need to do is subclass UITableView , and override hitTest:withEvent: 您需要做的只是UITableView子类,并覆盖hitTest:withEvent:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if ([[super hitTest:point withEvent:event] isKindOfClass:[UIButton class]]) {
        UIButton *buttonThatWasTapped = (UIButton *)[super hitTest:point withEvent:event];
        [buttonThatWasTapped sendActionsForControlEvents:UIControlEventTouchUpInside];
    }
        return [super hitTest:point withEvent:event];
}

Here's what this code does: if the tableview is touched, and the touch occurred in the bounds of a UIButton, the tableview will forward the touch event to the UIButton. 以下是此代码的作用:如果触摸了tableview,并且触摸发生在UIButton的边界,则tableview会将触摸事件转发给UIButton。

There's a caveat here: the touch event may get sent to the UIButton several times (I'm seeing the call being made 3 times for each touch). 这里有一个警告:触摸事件可能会多次被发送到UIButton(我看到每次触摸都会进行3次通话)。 This code was sufficient for my needs, but if you're using this code in your app, watch out for that issue - you may have to modify the above code to account for this. 此代码足以满足我的需求,但如果您在应用中使用此代码,请注意该问题 - 您可能需要修改上述代码才能解决此问题。

This is what's supposed to happen. 这是应该发生的事情。

The user is used to this behavior - it allows them to stop the table view's scrolling when they see what they wanted to touch. 用户习惯于这种行为 - 它允许他们在看到他们想要触摸的内容时停止表格视图的滚动。

Imagine I was scrolling through a table looking for the row 'unicorns', and I was scrolling really fast (because it's a big table, and I'm not sure where unicorns is). 想象一下,我正在滚动一张桌子寻找行'独角兽',我滚动速度非常快 (因为它是一张大桌子,而且我不确定独角兽在哪里)。 When 'unicorns' crosses my vision, I will instinctively stop the table to select it. 当'独角兽'穿过我的视线时,我会本能地停下桌子来选择它。

With the current system, I can tap anywhere on the table view and it will stop. 使用当前系统,我可以点击表格视图中的任何位置,它将停止。 If table views were implemented your way, I would tap 'underground rockchucks' to stop the table, and all of a sudden I am looking at information about rockchucks - and I want information about Unicorns. 如果用你的方式实现表格视图,我会点击“地下rockchucks”来停止桌子,突然间我正在查看有关rockchucks的信息 - 我想了解有关Unicorns的信息。

I now become a disgruntled user, return your app to Apple, and get my $0.99 back. 我现在成为一个心怀不满的用户,将你的应用程序返回给Apple,并获得0.99美元的回报。

You don't want users doing that. 您不希望用户这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM