所有视图尺寸的宽度仍然相同。我们希望弧线改变它们的大小,就好像它们被赋予了约束一样。
@IBDesignable class ArcView: UIView {
typealias ArcAction = () -> Void
struct ArcInfo {
var outlinePath: UIBezierPath
var action: ArcAction
}
private var arcInfos: [ArcInfo]!
let bgShapeLayer = CAShapeLayer()
var redRadius: Float?
var greenRadius: Float?
private var pathLineWidth: CGFloat {
return frame.size.width / 15.0
}
private var normalArcRadius: CGFloat {
return frame.size.width / 2 - pathLineWidth * 1.5
}
private var tappedArcRadius: CGFloat {
return frame.size.width / 2 - pathLineWidth / 2
}
required init?(coder: NSCoder) {
super.init(coder: coder)
let recognizer = UITapGestureRecognizer(target: self, action: #selector(tap(_ :)))
addGestureRecognizer(recognizer)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
override func draw(_ rect: CGRect) {
let fullCircle = CGFloat.pi * 2
let arcAngle = fullCircle * 1.5 / 6
var lastArcAngle = CGFloat.pi / 4.0 + CGFloat.pi //-CGFloat.pi
// background
let backPath = UIBezierPath(arcCenter: CGPoint(x: rect.width/2, y: rect.height/2), radius: 55.0, startAngle: 0, endAngle: CGFloat.pi * 2, clockwise: true)
#colorLiteral(red: 0.9931474328, green: 0.9932896495, blue: 0.9931163192, alpha: 1).setStroke()
backPath.lineWidth = 2.3
backPath.stroke()
#colorLiteral(red: 0.9212146401, green: 0.9490351081, blue: 0.9671724439, alpha: 1).setFill()
backPath.fill()
arcInfos = []
// Red Arc
func redArc( action: @escaping ArcAction) {
let path = UIBezierPath(arcCenter: CGPoint(x: rect.width/2, y: rect.height/2), radius: 46, startAngle: lastArcAngle, endAngle: lastArcAngle + arcAngle, clockwise: true)
#colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1).setStroke()
path.lineWidth = 10
path.stroke()
lastArcAngle += arcAngle
// separators
#colorLiteral(red: 0.927436769, green: 0.9490308166, blue: 0.967099607, alpha: 1).setStroke()
let outlinePath = hitTestPath(for: path)
outlinePath.lineWidth = 3
outlinePath.stroke()
arcInfos.append(ArcInfo(outlinePath: outlinePath, action: action))
}
//Green Arc
func greenArc( action: @escaping ArcAction) {
let path = UIBezierPath(arcCenter: CGPoint(x: rect.width/2, y: rect.height/2), radius: CGFloat(greenRadius ?? 56), startAngle: lastArcAngle, endAngle: lastArcAngle + arcAngle, clockwise: true)
#colorLiteral(red: 0.2443430722, green: 0.800511539, blue: 0.4006313086, alpha: 1).setStroke()
path.lineWidth = 10
path.stroke()
lastArcAngle += arcAngle
// separators
#colorLiteral(red: 0.927436769, green: 0.9490308166, blue: 0.967099607, alpha: 1).setStroke()
let outlinePath = hitTestPath(for: path)
outlinePath.lineWidth = 3
outlinePath.stroke()
arcInfos.append(ArcInfo(outlinePath: outlinePath, action: action))
}
//Add Arc
greenArc {
self.redRadius = 46
self.greenRadius = 56
}
redArc {
self.redRadius = 56
self.greenRadius = 46
}
}
@objc func tap(_ recognizer: UITapGestureRecognizer) {
let location = recognizer.location(in: self)
if let hitPath = (arcInfos.first { $0.outlinePath.contains(location) }) {
hitPath.action()
setNeedsDisplay()
//print(hitPath)
}
}
func hitTestPath(for path: UIBezierPath) -> UIBezierPath {
let pathCopy = path.cgPath.copy(strokingWithWidth: 15, lineCap: .butt, lineJoin: .miter, miterLimit: 0)
return UIBezierPath(cgPath: pathCopy)
}
}
protocol circleViewDelegate: NSObjectProtocol {
func selectedType(_ type: typeCircle)
}
我以之前的答案为基础,重写了一遍,结果如下