一个自定义图表的需求,顺便分享一下
设置点击区域,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)
}