2017-01-05 71 views
0

如何創建一個堆疊進度指示器,以便與來自存儲管理的界面生成器一起使用?堆棧進度指示器swift

enter image description here

是否有任何類似的可可控制?

如果沒有,我應該嘗試創建一個自定義視圖,在女巫我加我每次打電話一樣progress.add(width: 10.0, color: NSColor(...))?

+0

您可以嘗試將多個進度條分層顯示爲透明軌道色調,並相應地設置每個小節的進度。 – Callam

回答

2

的方法我只是做了一個函數來畫一些意見的時間內一個新的觀點,你可以玩一下。

func drawStackedProgress(percentages:[Float], width:Float, height:Float, x:Float, y:Float){ 
     var currentX = x 

     // I just threw a bunch of random (mostly probably ugly) colors in this array. Go ahead and make sure there's as many colors as stacked elements. 
     var colors = [UIColor.red, UIColor.blue, UIColor.green, UIColor.brown, UIColor.cyan] 
     var index = -1 
     for percentage in percentages{ 
      index += 1 
      let DynamicView=UIView(frame: CGRect(x: CGFloat(currentX), y: CGFloat(y), width: CGFloat(Double(percentage)*Double(width)), height: CGFloat(height))) 
      currentX+=Float(Double(percentages[index])*Double(width)) 
      DynamicView.backgroundColor=colors[index] 
      self.view.addSubview(DynamicView) 
     } 

    } 

實現:

drawStackedProgress(percentages: [0.1,0.3,0.5,0.1], width: 200, height: 20, x: 200, y: 200) 
1

更新:對於Mac應用程序轉換的代碼。 更正了索引超出範圍的顏色。

public func drawStackedProgress(percentages: [CGFloat], width: CGFloat, height: CGFloat, x: CGFloat, y: CGFloat){ 
     var currentX = x 
     var colors = [NSColor.red, NSColor.blue, NSColor.green, NSColor.brown, NSColor.cyan] 
     var index = -1 
     for percentage in percentages{ 
      index += 1 
      let DynamicView = NSView(frame: CGRect(x: currentX, y: y, width: percentage * width, height: height)) 
      currentX += percentages[index] * width 
      // if the colors have been all used, it starts choosing again from the first 
      DynamicView.layer?.backgroundColor = colors[index % colors.count].cgColor 
      self.addSubview(DynamicView) 
     } 
    }