我有一个要求,我需要显示多条进度线可以是n行.

当前行应采用渐变色,前一行应采用绿色,下一行应采用灰色.

Image for reference

我期待着一个类似于添加的图像的视图,一切都应该从故事板完成,我不想在我的视图控制器类上有任何出口.

推荐答案

您好,您可以使用下面提到的代码来实现结果:

@IBDesignable
class PageProgressView: UIView {
    
    @IBInspectable var statusLines: Int = 1 {
        didSet {
            setupStatusLines()
        }
    }
    
    @IBInspectable var currentLineIndex: Int = 0 {
        didSet {
            setupStatusLines()
        }
    }
    
    
    @IBInspectable var previousColor: UIColor = UIColor(red: 0.44, green: 0.81, blue: 0.59, alpha: 1) {
        didSet {
            setupStatusLines()
        }
    }
    
    
    @IBInspectable var nextColor: UIColor = UIColor(red: 0.22, green: 0.22, blue: 0.31, alpha: 1) {
        didSet {
            setupStatusLines()
        }
    }
    
    @IBInspectable var gradientStart: UIColor = UIColor(resource: .secondary) {
        didSet {
            setupStatusLines()
        }
    }
    
    @IBInspectable var gradientEnd: UIColor = UIColor(resource: .primary) {
        didSet {
            setupStatusLines()
        }
    }
    
    
    private let statusLineHeight: CGFloat = 2
    private let statusLinePadding: CGFloat = 10.0
    private let lineSpacing: CGFloat = 6.0
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupView()
    }
    
    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        setupView()
    }
    
    private func setupView() {
        backgroundColor = UIColor.white // Set background color
        
        // Add any additional setup for the view here
    }
    
    private func setupStatusLines() {
        // Remove any existing status lines
        subviews.forEach { $0.removeFromSuperview() }
        
        let numberOfLines = CGFloat(statusLines)
        let totalSpacing = lineSpacing * (numberOfLines - 1)
        let availableWidth = bounds.width - (2 * statusLinePadding) - totalSpacing
        let lineWidth = availableWidth / numberOfLines
        
        var currentX: CGFloat = statusLinePadding
        
        // Add status lines
        for index in 1...statusLines {
            let label = UILabel()
            label.text = ""
            label.font = UIFont.systemFont(ofSize: 16.0)
            label.numberOfLines = 1
            label.textAlignment = .center
            label.frame = CGRect(x: currentX, y: 0, width: lineWidth, height: statusLineHeight)
            label.layer.cornerRadius = statusLineHeight / 2
            label.clipsToBounds = true
            // Apply background colors
            
            if index < currentLineIndex {
                label.backgroundColor = previousColor // Green for previous line
            } else if index > currentLineIndex {
                label.backgroundColor =  nextColor// Grey for next line
            } else {
                label.backgroundColor = gradientColor() // Apply gradient color to the current line
            }
            
            addSubview(label)
            currentX += lineWidth + lineSpacing
        }
    }
    
    private func gradientColor() -> UIColor {
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = bounds
        gradientLayer.colors = [gradientStart.cgColor, gradientEnd.cgColor] // Customize your gradient colors here
        gradientLayer.startPoint = CGPoint(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint(x: 0.4, y: 0)
  
        
        UIGraphicsBeginImageContextWithOptions(gradientLayer.bounds.size, gradientLayer.isOpaque, 0.0)
        gradientLayer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return UIColor(patternImage: image!)
    }
}

You can set colours and other properties from storyboard check here here

Ios相关问答推荐

为什么导航栏在与Style.Page的选项卡栏一起使用时停止工作?

如何将Safari调试器附加到Turbo-iOS原生应用中的模拟器

快速相机放大手势不能正常工作

自定义UIControl不适用于UITapGestureRecognizer

如何在KMM项目中处理PHPickerViewController的回调?

当目标位于菜单标签内时,matchedGeometryEffect 的行为很奇怪

Flutter Aging Person 自定义小部件

如何将 -fobjc-arc-exceptions 标志正确添加到 XCode 中?

如何解码没有名称的 JSON 数组?

如何使用 AppIntents 在 SwiftUI 应用程序中打开特定视图

如何在字段包含字典数组的firestore上查询?

JSONDecoder 在模拟器上运行良好时无法在真实设备上解码

UICollectionView - 水平滚动,水平布局?

UICollectionView 添加 UICollectionCell

UITableView 页脚,停止浮动内容

如何将 iPhone OSStatus 代码转换为有用的东西?

从 WKWebView 获取所有 cookie

SwiftUI - 列表行中的多个按钮

使用 Chrome DevTools 调试 iOS 6+7 Mobile Safari

如何在 iOS 中获取正在运行的应用程序的名称