【问题标题】:UIButton in UITableViewCell doesn't highlight on tapUITableViewCell 中的 UIButton 在点击时不会突出显示
【发布时间】:2014-03-10 00:37:31
【问题描述】:

问题:当用户点击 UITableViewCell 中的 UIButton 时,该按钮只会在长按时突出显示,而不是在快速点击时突出显示。无论点击持续时间长短,此按钮都会突出显示的所需行为。

不幸的是:在任何 UIScrollView 或 UITableView 上将 delaysContentTouches 设置为 NO 是不可行的,因为会产生其他不良副作用。

所以:我怎样才能解决这个问题 - 有没有办法将触摸转发到按钮,绕过 delaysContentTouches 值?

【问题讨论】:

    标签: ios iphone objective-c ipad uibutton


    【解决方案1】:

    只需将delayContentTouches = false 设置为您正在使用的任何滚动视图(UITableView 或 UICollectionView)。应该这样做。

    正如文档所说:

    一个布尔值,用于确定滚动视图是否延迟对触摸手势的处理。 如果此属性的值为 true,则滚动视图会延迟处理触摸向下手势,直到它可以确定滚动是否是意图。如果值为 false ,滚动视图会立即调用 touchesShouldBegin(_:with:in:)。默认值为 true。

    【讨论】:

      【解决方案2】:

      这对我有用:

          @weakify(self);
      [self.publishButton bk_addEventHandler:^(id sender) {
          @strongify(self);
          if (self.clickBlock) {
              self.clickBlock(self.draftModel);
          }
          [UIView animateWithDuration:0.1 animations:^{
              self.publishButton.backgroundColor = [UIColor whiteColor];
          }];
      } forControlEvents:UIControlEventTouchUpInside];
      
      [self.publishButton bk_addEventHandler:^(id sender) {
          self.publishButton.backgroundColor = [UIColor redColor];
      } forControlEvents:UIControlEventTouchDown];
      

      【讨论】:

        【解决方案3】:

        在原型单元格中将按钮的标签设置为“1”。

        在你的 cellForRowAtIndexPath 中,你应该将 UIButton 链接到一个方法:

        UIButton *button = (UIButton *)[cell viewWithTag:1];
        [button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];
        

        那么在方法中你所做的就是:

        -(void) aMethod: (id) sender{
        
        
            CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.cartTableView];
            NSIndexPath *indexPath = [self.cartTableView indexPathForRowAtPoint:buttonPosition];
            if (indexPath != nil)
             {
                //Do your stuff
            }
        }
        

        这不会在运行代码之前增加任何延迟。

        【讨论】:

        • OP 询问如何在快速点击时突出显示按钮。你的建议并没有这样做。
        【解决方案4】:

        您需要创建一个 UIButton 类别(或子类,如果您不想影响所有其他按钮),并在 touchesBegan 中设置 highlight = YES。

        请参阅this answer 中的代码以获取示例实现。

        【讨论】:

          【解决方案5】:

          使用主线程。

              dispatch_async(dispatch_get_main_queue(), ^{
          
              });
          

          【讨论】:

            猜你喜欢
            • 2017-07-06
            • 2017-05-20
            • 2019-11-30
            • 2013-07-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多