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, with event: UIEvent?) {
        isHighlighted = true
        super.touchesBegan(touches, with: event)
    }

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

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

你可能感兴趣的:(UITableViewCell中的Button的高亮问题)