0
我試圖從某個開始角度到某個結束角度實施弧形繪製。我正在嘗試繪製弧形圖,但沒有運氣。iOS Swift - 弧形填充的動畫繪製,不是邊框筆劃
我已經找了幾個實現,但他們都指的是圈的動畫boder繪圖。
像繪製一個餡餅,但與一種顏色。有一定的開始和結束角度。餡餅的動畫。
我喜歡的是在從起始角度到結束角度動畫弧時有動畫填充。
像弧一樣正在順時針繪製,並在繪製時填充其內部顏色。
任何想法我如何在Swift中實現這一點?
在此先感謝。
編輯
這是我得出:
(灰色表示通過繪製餅覆蓋的區域)
代碼繪製餅圖
import Foundation
import UIKit
@IBDesignable class PieChart : UIView {
@IBInspectable var percentage: CGFloat = 50 {
didSet {
self.setNeedsDisplay()
}
}
@IBInspectable var outerBorderWidth: CGFloat = 2 {
didSet {
self.setNeedsDisplay()
}
}
@IBInspectable var outerBorderColor: UIColor = UIColor.white {
didSet {
self.setNeedsDisplay()
}
}
@IBInspectable var percentageFilledColor: UIColor = UIColor.primary {
didSet {
self.setNeedsDisplay()
}
}
let circleLayer = CAShapeLayer()
override func draw(_ rect: CGRect) {
drawPie(rect: rect, endPercent: percentage, color: UIColor.primary)
}
func drawPie(rect: CGRect, endPercent: CGFloat = 70, color: UIColor) {
let center = CGPoint(x: rect.origin.x + rect.width/2, y: rect.origin.y + rect.height/2)
let radius = min(rect.width, rect.height)/2
let gpath = UIBezierPath(arcCenter: center, radius: radius, startAngle: 0, endAngle: CGFloat(360.degreesToRadians), clockwise: true)
UIColor.white.setFill()
gpath.fill()
let circlePath = UIBezierPath(arcCenter: center, radius: radius, startAngle: CGFloat(0), endAngle:CGFloat(360.degreesToRadians), clockwise: true)
circleLayer.path = circlePath.cgPath
circleLayer.fillColor = UIColor.clear.cgColor
circleLayer.strokeColor = outerBorderColor.cgColor
circleLayer.lineWidth = outerBorderWidth
circleLayer.borderColor = outerBorderColor.cgColor
self.layer.addSublayer(circleLayer)
let π: CGFloat = 3.14
let halfPi: CGFloat = π/2
let startPercent: CGFloat = 0.0
let startAngle = (startPercent/100 * π * 2 - π) + halfPi
let endAngle = (endPercent/100 * π * 2 - π) + halfPi
let path = UIBezierPath()
path.move(to: center)
path.addArc(withCenter: center, radius: radius, startAngle: CGFloat(startAngle), endAngle: CGFloat(endAngle), clockwise: true)
path.close()
percentageFilledColor.setFill()
path.fill()
}
}
創建UIView的子類,以便您可以繪製特定程度的餅圖。 –
@ElTomato請參閱編輯。 –
PieChart需要一個具有結束角度值的初始化程序,以便它可以被初始化。然後創建另一個類來創建PieChart對象,並逐漸更改結束值。 –