我使用UIBezier Path绘制了一个带有此代码的分段圆圈:
func createCircleWithStartAndgle(startAngle: CGFloat, endAngle: CGFloat, chartSection: Int) {
var subView = circleView
var radius: CGFloat = circleView.bounds.size.width / 2 - 20
var circleShapeLayer = CAShapeLayer()
var arcCenter = CGPoint(x: radius, y: radius)
var bezierPath = UIBezierPath(arcCenter: arcCenter, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
circleShapeLayer.path = bezierPath.cgPath
circleShapeLayer.position = CGPoint(x:(subView?.bounds.midX)! - radius , y: (subView?.bounds.midY)! - radius)
circleShapeLayer.fillColor = UIColor.clear.cgColor
var strokeColor = UIColor()
switch chartSection {
case 0:
strokeColor = .red
firstLayer = circleShapeLayer
fPath = bezierPath
case 1:
strokeColor = .yellow
secondLayer = circleShapeLayer
sPath = bezierPath
case 2:
strokeColor = .blue
thirdLayer = circleShapeLayer
thPath = bezierPath
default:
strokeColor = .gray
}
circleShapeLayer.strokeColor = strokeColor.cgColor
circleShapeLayer.lineWidth = 10
subView?.layer.insertSublayer(circleShapeLayer, at: UInt32(chartSection))
}
现在我希望能够触及每个细分并识别细分,并使其更大。
当我使用此代码检测触摸时,它适用于红色和黄色段(第一和第二路径),但为了识别蓝色段,我需要点击左上角或稍微向上一点蓝色部分。如果我点击片段本身,它会检测到它,因为它是第一段,而不是第三段。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
guard let point = touch?.location(in: circleView) else { return }
guard let sublayers = circleView.layer.sublayers as? [CAShapeLayer] else { return }
for layer in sublayers {
if let path = layer.path {
if path == fPath.cgPath && path.contains(point) {
print("first segment")
} else if path == sPath.cgPath && path.contains(point) {
print("second segment")
} else if path == thPath.cgPath && path.contains(point) {
print("third segment")
}
}
}
}
有什么问题?如何正确检测点击并修改tapped bezierPath的strokWidth?