AjayBhanushali / ABGaugeViewKit

ABGaugeViewKit is a framework which will help you to add Gauge View in your iOS App.
MIT License
27 stars 25 forks source link

Programatic Implementation #4

Closed Tydewest closed 3 years ago

Tydewest commented 5 years ago

Is It possible to create the gauge programatically

NoahKamara commented 5 years ago

Ive only tried with swiftUI but should be the same as creating any other View programmatically. in SwiftUI you use UIRepresentable like this:

struct FuelGauge: UIViewRepresentable {
    var percentage: Double
    func makeUIView(context: Context) -> ABGaugeView {
        let view = ABGaugeView(frame: .zero)
        view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.arcAngle = 1.8
        view.applyShadow = false
        view.isRoundCap = false
        view.blinkAnimate = true
        view.areas = "25,50,25" //Have to be String!
        view.colorCodes = "FF2600,FFFB00,00F900"
        view.backgroundColor = .gray
        return view
    }

    func updateUIView(_ view: ABGaugeView, context: Context) {
        view.needleValue = CGFloat(percentage*100)
    }
}