堆叠进度指示器swift

堆叠进度指示器swift,swift,cocoa,progress-bar,Swift,Cocoa,Progress Bar,如何创建一个堆叠的进度指示器,以便与存储管理中的界面生成器一起使用 是否有类似的可可管制 如果没有,我是否应该尝试创建一个自定义视图,在这种情况下,每次调用progress.addwidth:10.0,color:NSColor…?之类的方法时,我都会在其中添加一个新视图。我刚刚创建了一个函数来绘制一些视图,您可以稍微使用它 func drawStackedProgress(percentages:[Float], width:Float, height:Float, x:Float, y:Fl

如何创建一个堆叠的进度指示器,以便与存储管理中的界面生成器一起使用

是否有类似的可可管制


如果没有,我是否应该尝试创建一个自定义视图,在这种情况下,每次调用progress.addwidth:10.0,color:NSColor…?

之类的方法时,我都会在其中添加一个新视图。我刚刚创建了一个函数来绘制一些视图,您可以稍微使用它

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)

我只是做了一个函数来画一些视图,你可以玩一下

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)

更新: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)
        }
    }

更新: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)
        }
    }
我制作了一个简单的吊舱,你可以检查一下:

我制作了这个简单的吊舱,您可以查看:


您可以尝试将多个进度条以透明的轨道色调分层,并相应地设置每个进度条的进度。您可以尝试将多个进度条以透明的轨道色调分层,并相应地设置每个进度条的进度。