2016-05-18 17 views
1

任何想法如何畫一個圓環圖,像這樣的標題:繪製圓環圖與職稱的斯威夫特

enter image description here

我試過幾個庫,但沒有成功。我無法在每個切片之間創建這些白線。下劃線標題對我來說太難了。任何幫助將不勝感激!先謝謝你!

回答

4

您可以使用CABasicAnimation畫弧,例如

[沒有經過測試的代碼]

// e.g. 0.3 is 30% 
func calculateEndAngle(percentageAsDouble: Double) -> CGFloat { 
    let endAngle:CGFloat = CGFloat(2 * M_PI - (M_PI * percentageAsDouble)) 
    return endAngle 
} 

let animationCenter:CGPoint = self.view.center 
let animationRadius:CGFloat = 100.0 
let animationLineWidth:CGFloat = 16.0 
let endAngle = calculateEndAngle(0.3) 
let arcPath = UIBezierPath(arcCenter: animationCenter, radius: animationRadius, startAngle: CGFloat(M_PI), endAngle:endAngle, clockwise: true) 

let arcLayer = CAShapeLayer() 
arcLayer.path = arcPath.CGPath 
arcLayer.fillColor = UIColor.clearColor().CGColor 
arcLayer.strokeColor = UIColor.greenColor().CGColor 
arcLayer.lineWidth = animationLineWidth 

self.view.layer.addSublayer(arcLayer) 
+0

謝謝!這很好,但是如何根據某些數據計算角度? 像,有三個值 - 30%,50%和20%?如何用它們之間的白線繪製這三個切片? – scourGINHO

+0

所以整圓是2 * M_PI,所以你需要保留一個開始和結束值的列表,每個正確的弧長(0.3 * 2 * M_PI是30%,但你需要根據30 %開始)。 – Damo

+0

你能否發表一段代碼來表明這一點? 使用虛擬數據 - 50%,30%,20%。無論從哪裏開始。 – scourGINHO