问题描述
private(set) var animationTime: [CFTimeInterval] = [1,1,1]
private(set) var values: [Double] = [0,0]
private(set) var colors: [UIColor] = [UIColor.red,UIColor.blue,UIColor.yellow]
private(set) var radiusRatios: [Double] = [1,1]
private(set) var dataSize: Int {
get {
return values.count
}
set {
while animationTime.count > newValue {
animationTime.removeLast()
}
while animationTime.count < newValue {
animationTime.append(1)
}
while values.count > newValue {
values.removeLast()
}
while values.count < newValue {
values.append(1)
}
while colors.count > newValue {
colors.removeLast()
}
while colors.count < newValue {
colors.append(UIColor.red)
}
while radiusRatios.count > newValue {
radiusRatios.removeLast()
}
while radiusRatios.count < newValue {
radiusRatios.append(1)
}
}
}
func setCircle(number index: Int,data: Double) {
values[index] = data
}
var drawing = false
var animationoption = UIView.Animationoptions()
var maximumAnimationDuration: TimeInterval = 0
override public func draw(_ rect: CGRect) {
if !drawing {
drawing = true
var dataInPercentage = [Double]()
var max = Double(0)
for value in values {
max += value
}
for value in values {
dataInPercentage.append(value / max)
}
layer.sublayers?.removeAll()
layer.mask?.removeAllAnimations()
var startAngle = CGFloat(0)
var endAngle = CGFloat(0)
for index in 0 ..< dataSize {
endAngle = startAngle + CGFloat(dataInPercentage[index] * 2) * CGFloat.pi
let circle = CAShapeLayer()
layer.addSublayer(circle)
circle.position = CGPoint(x: layer.bounds.minX,y: layer.bounds.midY)
let circleCenter = CGPoint(x: 0,y: rect.width / 2)
let circularPath = UIBezierPath()
circularPath.move(to: circleCenter)
circularPath.addArc(withCenter: circleCenter,radius: CGFloat(radiusRatios[index]) * rect.width / 2,startAngle: startAngle,endAngle: endAngle,clockwise: true)
startAngle = endAngle
circularPath.close()
circle.path = circularPath.cgPath
circle.strokeColor = UIColor.gray.cgColor // edited
circle.linewidth = 1
circle.lineJoin = .round
circle.fillColor = colors[index].cgColor
circle.transform = CATransform3DMakeRotation(-CGFloat.pi / 2,1)
}
let maskLayer = CAShapeLayer()
let coverPath = UIBezierPath(roundedRect: rect,cornerRadius: rect.width / 2)
maskLayer.path = coverPath.cgPath
maskLayer.strokeEnd = 0
maskLayer.frame = rect
maskLayer.fillColor = UIColor.clear.cgColor
maskLayer.strokeColor = UIColor.gray.cgColor // edited
maskLayer.linewidth = rect.width
centerPieCircleheight = rect.height
centerPieCircleWidth = rect.width
layer.mask = maskLayer
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.duration = 1
animation.fromValue = 0.0
animation.tovalue = 1.0
animation.fillMode = camediatimingFillMode.forwards
animation.isRemovedOnCompletion = false
maskLayer.add(animation,forKey: "PieChartOverlayer")
UIView.transition(with: self,duration: animation.duration,options: animationoption,animations: nil,completion: { [weak self] _ in
self?.drawing = false
})
}
}
以及我的使用方式
首先,我设置数据大小:
activityChart.set(dataSize: 3)
比在其父控制器中添加值
public func setValues(number: Int,data: Double) {
activityChart.setNeedsdisplay()
activityChart.setCircle(number: number,data: data)
}
并且您知道,当有新值出现时,我必须使用setNeedsdisplay
刷新图表,但是当我这样做时,整个图表将再次顺时针绘制,这是不好的,尤其是当图表应该每隔几秒钟刷新一次。
谁能建议我一种方法来管理它以显示新数据,而无需再次使用动画(我更喜欢)甚至没有动画绘制。
非常感谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)