美文网首页
swift hittest扩大点击范围

swift hittest扩大点击范围

作者: 松龄学编程 | 来源:发表于2020-05-17 10:15 被阅读0次
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let moreButtonFrame = moreButton.frame.inset(by: UIEdgeInsets(top: -10, left: -10, bottom: -10, right: -10))
        if moreButtonFrame.contains(point) {
            return moreButton
        } else {
            return super.hitTest(point, with: event)
        }
    }

如果按钮很多,可以子类butto

class XXButton: UIButton {
    var margin: CGFloat = 5
    
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        let area = self.bounds.insetBy(dx: -margin, dy: -margin) //负值是方法响应范围
        return area.contains(point)
    }
}

相关文章

网友评论

      本文标题:swift hittest扩大点击范围

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