搜索
您的当前位置:首页正文

UIbutton 在 scrollerView 中高亮(isHi

来源:二三娱乐

在项目中使用 button 点击时会出现高亮延迟的现象.经测试发现,该现象出现在继承自 UIScrollerView的视图中.
经过查询得知:scrollerView 有一个属性是delaysContentTouches.下面是官方的解释:

If the value of this property is YES, the scroll view delays handling the touch-down gesture until it can determine if scrolling is the intent. If the value is NO, the scroll view immediatelycalls touchesShouldBegin:withEvent:inContentView:. The default value is YES.

意思是这个属性默认值是 YES. 滚动视图会延迟触摸手势,直到它确定是否滚动是意图.因此才会有150ms 的延迟.这样的话我们就会在点击的时候看不到高亮效果,只有长按的时候才会有高亮的效果.
这时候我们必须将 button 所在的 scroller 的delaysContentTouches属性设置为 false.

我们还会发现只设置这个属性对于 UItableView 是无效的
这又是为什么呢?

tableview
下面我们看一下图层结构,会注意到 cell 是放在一个叫 UITableViewWrapperView的图层中,看一下左边的图标我们不难发现这个view 其实是一个 scrollerView. 那么问题就迎刃而解了.
      fileprivate func resubview(view: UIView) {
            for subview in view.subviews {
                if subview.subviews.count == 0 {
                    if subview.isKind(of: UIScrollView.classForCoder()) {
                        (subview as! UIScrollView).delaysContentTouches = false
                    }
                } else {
                    resubview(view: subview)
                }
            }
        }

通过遍历 tableView 的图层找到继承自 UIScrollerView 的图层.将该图层的属性也设置一下.

之后我们也许会遇到另一个问题.当滑动时如果手势的起始位置是落在有手势的控件内,那么久会影响滑动.
UIScrollView中有一个方法:func touchesShouldCancel(in view: UIView) -> Bool就是解决该问题的.下面是官方文档的解释:

所以要注意设置canCancelContentTouchesYES.

Top