一個(gè)自定義圖表的需求,順便分享一下

6312E625-29F3-499F-8E71-DC332CBA91BC.png
設(shè)置點(diǎn)擊區(qū)域,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 {
//內(nèi)部點(diǎn)位相對(duì)的坐標(biāo)系為視圖本身
//左側(cè)起始點(diǎn)
let leftbeginpoint = CGPoint(x: 0, y: self.frame.size.height)
//中間點(diǎn)
let centerpoint = CGPoint(x: self.frame.size.width/2, y: 0)
//控制點(diǎn)
let controlpoint = CGPoint(x: self.frame.size.width/2,
y: self.frame.size.height*6/7)
//右側(cè)起始點(diǎn)
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
}
}
設(shè)置layer,可在自定義button里設(shè)置,但是目前不會(huì)做-。-
//制作三角柱
func creatTriangleColumn(columnlable: CusButton){
//內(nèi)部點(diǎn)位相對(duì)的坐標(biāo)系為視圖本身
//左側(cè)起始點(diǎn)
let leftbeginpoint = CGPoint(x: 0, y: columnlable.frame.size.height)
//中間點(diǎn)
let centerpoint = CGPoint(x: columnlable.frame.size.width/2, y: 0)
//控制點(diǎn)
let controlpoint = CGPoint(x: columnlable.frame.size.width/2,
y: columnlable.frame.size.height*6/7)
//右側(cè)起始點(diǎn)
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)
}