大家好,我是Coregraphics和CoreAnimations的新手。谁能建议我在饼图上方绘制文本时做错了什么地方。
我已使用以下代码绘制了半圆形状。
override func draw(_ rect: CGRect) {
let radius = min(bounds.size.width, bounds.size.height) / 2;
let center = CGPoint.init(x: bounds.size.width, y: bounds.size.height / 2)
let selectedSliceIndex = 4;
let sliceCount = 6;
let slicePath = UIBezierPath.init()
let selectedSlicePath = UIBezierPath.init()
slicePath.lineWidth = 1;
slicePath.move(to: center)
var angle: CGFloat = 0 - (CGFloat.pi / 2);
let interval: CGFloat = (CGFloat.pi) / CGFloat(sliceCount)
for i in 0 ... sliceCount {
let x = center.x + (radius * cos(angle))
let y = center.y + (radius * sin(angle))
slicePath.addLine(to: CGPoint.init(x: x, y: y))
slicePath.move(to: center)
// button.center = CGPointMake(self.center.x + raduis * sin(start + layoutDegree/(icons.count-1)*idx), self.center.y + raduis * cos(start + layoutDegree/(icons.count-1)*idx));
// 1. create a gesture recognizer (tap gesture)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
// 2. add the gesture recognizer to a view
self.addGestureRecognizer(tapGesture)
let countLabel = UILabel()
countLabel.text = "Rakesh"
let size:CGFloat = 55.0
countLabel.textColor = UIColor.white
countLabel.textAlignment = .center
countLabel.font = UIFont.systemFont(ofSize: 14.0)
countLabel.frame = CGRect(x : 30 ,y : 0,width : 100, height : 40)
countLabel.layer.borderWidth = 3.0
countLabel.layer.masksToBounds = true
countLabel.layer.backgroundColor = UIColor.orange.cgColor
countLabel.layer.borderColor = UIColor.clear.cgColor
countLabel.center = CGPoint.init(x: center.x + (radius * cos(angle + interval)) , y: center.y + (radius * sin(angle + interval)))
// myLayer.frame = CGRect(x:center.x + (radius * cos(angle)), y: center.y + (radius * sin(angle)), width: 50, height: 40);
self.addSubview(countLabel)
// countLabel.transform = CGAffineTransform(rotationAngle: 60)
if i == selectedSliceIndex {
selectedSlicePath.move(to: center)
selectedSlicePath.addArc(withCenter: center, radius: radius, startAngle: angle - interval, endAngle: angle, clockwise: true)
selectedSlicePath.close()
}
angle -= interval;
}
// outer blue circle
let semiCirclePath = UIBezierPath.init()
semiCirclePath.move(to: center)
semiCirclePath.addArc(withCenter: center, radius: radius, startAngle: CGFloat.pi / 2, endAngle: CGFloat.pi + (CGFloat.pi / 2) , clockwise: true)
semiCirclePath.close()
UIColor.blue.setFill()
semiCirclePath.fill()
// lines
UIColor.white.setStroke()
slicePath.stroke()
// selected slice
UIColor(red: 67.0/255.0, green: 67.0/255.0, blue: 144.0/255.0, alpha: 1.0).setFill()
selectedSlicePath.fill()
UIColor.clear.setFill()
// inner gray circle
semiCirclePath.removeAllPoints()
semiCirclePath.move(to: center)
semiCirclePath.addArc(withCenter: center, radius: 30, startAngle: CGFloat.pi / 2, endAngle: CGFloat.pi + (CGFloat.pi / 2) , clockwise: true)
UIColor(red: 67.0/255.0, green: 67.0/255.0, blue: 144.0/255.0, alpha: 1.0).setFill()
semiCirclePath.fill()
}.
结果应该是
实际上,我想在具有相同角度切片的饼切片上方动态添加文本,但是我无法实现。请在下面找到屏幕截图: