2016-11-20 14 views
0

基本上我想創建一個計時器應用程序。這就是爲什麼我需要一個圓圈來顯示計時器的進度。如果定時器爲10秒,則該圓圈將被分成10段,每段在1秒後出現。好的。如何在swift中從第一象限分割圓圈

我已經成功地從SO post -> draw-a-circular-segment-progress-in-swift創建它,但有點問題。我的圈子的分段從第四個四分體開始,但不是預期的。我想要顯示來自第一個quadrent的段。在這裏我已經爲8段創建了這個函數。

請提供可以動態使用的建議。

func createCircle(){ 

     let circlePath = UIBezierPath(arcCenter: CGPoint(x: view.frame.size.width/2,y: view.frame.size.height/2), radius: CGFloat(90), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true) 


     let segmentAngle: CGFloat = (360 * 0.125)/360 

     for i in 0 ..< 8 { 

      let circleLayer = CAShapeLayer() 
      circleLayer.path = circlePath.CGPath 

      // start angle is number of segments * the segment angle 
      circleLayer.strokeStart = segmentAngle * CGFloat(i) //I think all is for this line of code. 

      print("\nStroke \(i): ",segmentAngle * CGFloat(i)) 

      // end angle is the start plus one segment, minus a little to make a gap 
      // you'll have to play with this value to get it to look right at the size you need 
      let gapSize: CGFloat = 0.008 

      circleLayer.strokeEnd = circleLayer.strokeStart + segmentAngle - gapSize 

      circleLayer.lineWidth = 10 

      circleLayer.strokeColor = UIColor(red:0, green:0.004, blue:0.549, alpha:1).CGColor 
      circleLayer.fillColor = UIColor.clearColor().CGColor 

      // add the segment to the segments array and to the view 
      segments.insert(circleLayer, atIndex: i) 

     } 

     for i in 0 ..< 8{ 
      view.layer.addSublayer(segments[i]) 
     } 
    } 

回答

4

您必須抵消你的開始角度:

circleLayer.strokeStart = segmentAngle * CGFloat(i) - M_PI/2.0 

雖然改造也可以做的伎倆,我不會建議使用一個是這種情況。如果您之後想要因爲其他原因(動畫等)而進行轉換,那麼您可能還需要考慮任何當前的轉換,這可能使事情變得更復雜。

編輯:

,我認爲你的一些其他代碼可能會關閉爲好。而不是弄清楚是怎麼回事,我說幹就幹,從頭重寫了它(在斯威夫特3語法):

let count: Int = 8 
let gapSize: CGFloat = 0.008 
let segmentAngleSize: CGFloat = (2.0 * CGFloat(M_PI) - CGFloat(count) * gapSize)/CGFloat(count) 
let center = CGPoint(x: view.frame.size.width/2.0, y: view.frame.size.height/2.0) 
let radius: CGFloat = 90 
let lineWidth: CGFloat = 10 
let strokeColor = UIColor(red:0, green:0.004, blue:0.549, alpha:1).cgColor 

for i in 0 ..< count { 
    let start = CGFloat(i) * (segmentAngleSize + gapSize) - CGFloat(M_PI/2.0) 
    let end = start + segmentAngleSize 
    let segmentPath = UIBezierPath(arcCenter: center, radius: radius, startAngle: start, endAngle: end, clockwise: true) 

    let arcLayer = CAShapeLayer() 
    arcLayer.path = segmentPath.cgPath 
    arcLayer.fillColor = UIColor.clear.cgColor 
    arcLayer.strokeColor = strokeColor 
    arcLayer.lineWidth = lineWidth 

    self.view.layer.addSublayer(arcLayer) 
} 

example code screenshot

+0

我用過你的線,但沒有線段。所以我打印你的價值,它顯示負值,但價值將[0,1] .....這就是爲什麼它不工作。 – KhanShaheb

+1

謝謝.....很多謝謝:) – KhanShaheb

0

使用旋轉變換來旋轉視圖/圖層,以便形狀圖層路徑的零點位於您想要的位置。

+0

對不起....我需要一個明顯的例子。 – KhanShaheb

+0

https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch12p566customThermometer/ch25p840customThermometer/MyCircularProgressButton.swift – matt