【问题标题】:Prevent UIButton press when scrolling滚动时防止按下 UIButton
【发布时间】:2012-06-26 22:57:32
【问题描述】:

我在表格单元格中有两个 UIButton,滚动时按下不便。我怎样才能防止这种情况?

我是否可以设置一些属性或发送事件,以便仅在用户释放按下时才按下按钮,而不是在按下按钮时按下?

我尝试了不同的触摸事件(Touch Up Inside 和 Touch Down),但似乎都没有解决这个问题。

【问题讨论】:

    标签: iphone ios uiscrollview uibutton


    【解决方案1】:

    您可以监听 tableview 的滚动委托回调并关闭正在滚动的按钮

    - (void)scrollViewWillBeginDragging:(UIScrollView *)activeScrollView {
    
        //I assume the buttons are within your cells so you will have to enable them within your cells so you will probably have to handle this by sending a custom message to your cells or accessing them with properties.
        [yourButton setEnabled: NO];
    }
    

    并倾听

    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    {
       // do the same to enable them back
       [yourButton setEnabled: YES];
    }
    

    【讨论】:

    • 如果您在滚动时按下按钮,这将起作用。我遇到的真正问题是,如果您在按钮顶部开始滚动手势,即使您在按钮之外释放它也会触发按钮。
    【解决方案2】:

    在界面生成器中,将按钮的标记值更改为大于 10 的数字(或某个值)。子类 UIScrollView 并覆盖:

    -(BOOL) touchesShouldCancelInContentView:(UIView *)view {
    
    if(view.tag>10) {
        NSLog(@"should A");
        return YES;
    } else return NO;
    

    }

    您的 scrollView 应该将属性 canCancelContentTouches 设置为 YES 并将 delaysContentTouches 设置为 NO

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-15
      • 2021-11-02
      • 2012-07-17
      • 2015-09-22
      • 2011-09-14
      • 1970-01-01
      • 2011-04-22
      • 1970-01-01
      相关资源
      最近更新 更多