UITableViewCell中的Button的高亮问题

放置了一个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)
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容