一個自定義圖表的需求露久,順便分享一下
6312E625-29F3-499F-8E71-DC332CBA91BC.png
設置點擊區(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)部點位相對的坐標系為視圖本身
//左側起始點
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){
//內(nèi)部點位相對的坐標系為視圖本身
//左側起始點
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)
}