BatteryView
  • September 11, 2023

Simple battery shaped UIView.

Usage


 

let batteryView = BatteryView(frame: smallRect)
batteryView.level = 42 // anywhere in 0…100
batteryView.lowThreshold = 25 // battery fill becomes red if level is below this threshold
batteryView.gradientThreshold = 50 // battery fill gradually changes from green to red below this threshold

SwiftUI Usage


BatteryShape(level: $level)

Changing Appearance


The properties below can be set in Interface Builder, in code, or through a UIAppearance proxy (e.g., BatteryView.appearance().borderColor = .gray). When using SwiftUI you can set them in the BatteryShape initializer .

Colors:

batteryView.borderColor = .darkGray
batteryView.highLevelColor = .green
batteryView.lowLevelColor = .red
batteryView.noLevelColor = .gray
batteryView.noLevelText = “?” // shown over battery when the level is undefined or out of bounds

Battery Shape:

batteryView.direction = .minXEdge // terminal facing left

batteryView.terminalLengthRatio = 0.1 // relative to battery length
batteryView.terminalWidthRatio = 0.4 // relative to battery width

batteryView.borderWidth = 2.5 // default is batteryLength / 20
batteryView.cornerRadius = 5 // default is batteryLength / 10

Installation


CocoaPods:

pod ‘BatteryView’

Swift Package Manager:

dependencies: [
.package(url: “https://github.com/yonat/BatteryView”, from: “1.4.1”)
]

GitHub


View Github

#battery #swift
YOU MIGHT ALSO LIKE...
SwiftUI Charts

Build custom charts with SwiftUI Styles      

SwiftUICharts

A simple line and bar charting library that support accessibility written using SwiftUI.

Swipeable cards

Swipeable Cards with SwiftUI

RGStack

This UI attempts to capture the Quibi Card Stack and the associated User Interaction.

🃏 CardStack

A easy-to-use SwiftUI view for Tinder like cards on iOS, macOS & watchOS.