美文网首页IOS开发体验
制作锥形按钮并重写UIbutton点击区域

制作锥形按钮并重写UIbutton点击区域

作者: LazyJ | 来源:发表于2020-11-12 09:47 被阅读0次

一个自定义图表的需求,顺便分享一下


6312E625-29F3-499F-8E71-DC332CBA91BC.png

设置点击区域,path为按钮的layer

class CusButton: UIButton {
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        let res = super.point(inside: point, with: event)
        if res {
            //内部点位相对的坐标系为视图本身
            
            //左侧起始点
            let leftbeginpoint = CGPoint(x: 0, y: self.frame.size.height)
            //中间点
            let centerpoint = CGPoint(x: self.frame.size.width/2, y: 0)
            //控制点
            let controlpoint = CGPoint(x: self.frame.size.width/2,
                                           y: self.frame.size.height*6/7)
            //右侧起始点
            let rightbeginpoint = CGPoint(x: self.frame.size.width,
                                          y: self.frame.size.height)
            
            let path = UIBezierPath()
            path.move(to: leftbeginpoint)
            path.addQuadCurve(to: centerpoint, controlPoint: controlpoint)
            path.addQuadCurve(to: rightbeginpoint, controlPoint: controlpoint)
            path.addLine(to: leftbeginpoint)
            path.close()
            
            if path.contains(point) {
                return true
            }
            
            return false
        }
        return false
    }
}

设置layer,可在自定义button里设置,但是目前不会做-。-

//制作三角柱
    func creatTriangleColumn(columnlable: CusButton){
        //内部点位相对的坐标系为视图本身
        //左侧起始点
        let leftbeginpoint = CGPoint(x: 0, y: columnlable.frame.size.height)
        //中间点
        let centerpoint = CGPoint(x: columnlable.frame.size.width/2, y: 0)
        //控制点
        let controlpoint = CGPoint(x: columnlable.frame.size.width/2,
                                       y: columnlable.frame.size.height*6/7)
        //右侧起始点
        let rightbeginpoint = CGPoint(x: columnlable.frame.size.width,
                                      y: columnlable.frame.size.height)
        
        
        let path = UIBezierPath()
        path.move(to: leftbeginpoint)
        path.addQuadCurve(to: centerpoint, controlPoint: controlpoint)
        path.addQuadCurve(to: rightbeginpoint, controlPoint: controlpoint)
        path.addLine(to: leftbeginpoint)
        path.close()
        let shapelayer = CAShapeLayer()
        shapelayer.path = path.cgPath
        shapelayer.fillColor = danblue3.cgColor
        columnlable.layer.addSublayer(shapelayer)
        
    }

相关文章

网友评论

    本文标题:制作锥形按钮并重写UIbutton点击区域

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