2017-01-05 24 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) 
     } 
    }