微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过重新绘制刷新饼图

如何解决通过重新绘制刷新饼图

我有一个饼图视图,下面是完整的代码

  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 举报,一经查实,本站将立刻删除。