美文网首页
UITableViewCell中的Button的高亮问题

UITableViewCell中的Button的高亮问题

作者: Mr_小伞 | 来源:发表于2017-08-23 10:03 被阅读47次

放置了一个UIButton到UITableViewCell当中,点击UIButton,UIButton没有高亮。

第一种解决方案:
UIScrollView 有一个 delaysContentTouches 属性,默认为YES,touch事件会被 delay。
那么把UITableView 的 delaysContentTouches 设置为 YES 应该就行了。

    private func setDelaysTouches() {
        tableView.delaysContentTouches = false
        for view in tableView.subviews {
            if let scroll = view as? UIScrollView {
                scroll.delaysContentTouches = false
            }
        }
    }

我在设置完上述的代码之后,虽然高亮的问题解决了。可是滚动TableView时,如果划过UIButton时,就会立马触发高亮效果,看着极其难受。

第二种解决方案:
在不设置delaysContentTouches属性为true的情况下,该如何做呢
自定义个HighlightedButton 就能完美解决啦。

class HighlightedButton: UIButton {

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        isHighlighted = true
        super.touchesBegan(touches, with: event)
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        isHighlighted = false
        super.touchesEnded(touches, with: event)
    }

    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        isHighlighted = false
        super.touchesCancelled(touches, with: event)
    }
}

相关文章

网友评论

      本文标题:UITableViewCell中的Button的高亮问题

      本文链接:https://www.haomeiwen.com/subject/wnfddxtx.html