我想要做的与MacOSiOS设备上的徽章几乎相同.我试图使用SFSymbols来实现这一点,但它只支持%0%25%50%75%100,但我想将我的电池徽章更新到当前的电池电量.

img

我试着在SFSymbols中使用battery.0图标,RoundedRectangle与它重叠,如下所示:

struct BatteryView : View {
    var body: some View {
            ZStack {
                Image(systemName: "battery.0")
                RoundedRectangle(cornerRadius: 15)
                    .foregroundColor(.green)
            }
    }
}

但当我因为RoundedRectangle分而试图扩大或缩小规模时,它的表现真的很奇怪.

提前谢谢!

推荐答案

这是我想出来的:

据我所知,SFSymbols中的battery图标只由3个元素组成,a rounded rectanglea half circle位于电池顶部,另外rounded rectangle表示当前电池电量.

First rounded Rectangle (Battery Case)

struct BatteryView : View {
    var body: some View {
        GeometryReader { geo in
            RoundedRectangle(cornerRadius: 15)
                .stroke(lineWidth: 3)
       } 
   }
}

它看起来是这样的(frame为180,60):

battery case

我只是按照你的建议用了一个圆角矩形作为电池盒.

Half circle

为此,我创建了一个名为HalfCircleShape的简单形状.

struct HalfCircleShape : Shape {
    func path(in rect: CGRect) -> Path {
        var path = Path()
        
        path.move(to: CGPoint(x: rect.minX, y: rect.midY))
        path.addArc(center: CGPoint(x: rect.minX, y: rect.midY), radius: rect.height , startAngle: .degrees(90), endAngle: .degrees(270), clockwise: true)
        return path
    }
}

GeometryReader帧中,我给出的帧是当前帧的1/7,因为它看起来很优雅.

我把这些和HStack

battery_2

如下所示:

GeometryReader { geo in
    HStack {
        RoundedRectangle(cornerRadius: 15)
            .stroke(lineWidth: 3)
        
        HalfCircleShape()
            .frame(width: geo.size.width / 7, height: geo.size.height / 7)
    }
}

Battery Indicator

我在Color中创建了一个扩展以 Select 当前电池电量的 colored颜色


extension Color {
    static var BatteryLevel : Color {
        let batteryLevel = 0.6
        print(batteryLevel)
        switch batteryLevel {
            // returns red color for range %0 to %20
            case 0...0.2:
                return Color.red
            // returns yellow color for range %20 to %50
            case 0.2...0.5:
                return Color.yellow
            // returns green color for range %50 to %100
            case 0.5...1.0:
                return Color.green
            default:
                return Color.clear
        }
    }
}

我刚刚创造了另一个RoundedRectangle,并将这两个RoundedRectangle结合在一个ZStack

Result looks 如下所示: red green yellow

最终代码:


import SwiftUI

struct ContentView: View {
    var body: some View {
            BatteryView()
            .frame(width: 170, height: 60)
            .padding()
    }
}

struct HalfCircleShape : Shape {
    func path(in rect: CGRect) -> Path {
        var path = Path()
        
        path.move(to: CGPoint(x: rect.minX, y: rect.midY))
        path.addArc(center: CGPoint(x: rect.minX, y: rect.midY), radius: rect.height , startAngle: .degrees(90), endAngle: .degrees(270), clockwise: true)
        return path
    }
}

struct BatteryView : View {
    init() {
        UIDevice.current.isBatteryMonitoringEnabled = true
    }
    var body: some View {
        // UIDevice.current.batteryLevel always returns -1, and I don't know why. so here's a value for you to preview
        let batteryLevel = 0.4
        GeometryReader { geo in
            HStack(spacing: 5) {
                GeometryReader { rectangle in
                    RoundedRectangle(cornerRadius: 15)
                        .stroke(lineWidth: 3)
                    RoundedRectangle(cornerRadius: 15)
                        .padding(5)
                        .frame(width: rectangle.size.width - (rectangle.size.width * (1 - batteryLevel)))
                        .foregroundColor(Color.BatteryLevel)
                }
                HalfCircleShape()
                .frame(width: geo.size.width / 7, height: geo.size.height / 7)
                
            }
            .padding(.leading)
        }
    }
}

extension Color {
    static var BatteryLevel : Color {
        let batteryLevel = 0.4
        print(batteryLevel)
        switch batteryLevel {
            // returns red color for range %0 to %20
            case 0...0.2:
                return Color.red
            // returns yellow color for range %20 to %50
            case 0.2...0.5:
                return Color.yellow
            // returns green color for range %50 to %100
            case 0.5...1.0:
                return Color.green
            default:
                return Color.clear
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

此外,在UIDevice中,有一个名为batteryLevel的属性可以告诉您当前的电池电量,但在本例中,它只返回-1,注意:as discussed here,这看起来像是SWIFT中的错误.

Ios相关问答推荐

滚动时突出显示工具栏项目

更改文本在Flutter 中的位置

删除领域中的cartItem时出现问题

如何在SwiftUI中以一定Angular 绘制直线(&A)

如何创建具有SPM依赖项的二进制XCFramework?

IOS:在UI线程中执行代码的闭包样式的替代

UITableViewCell编程约束问题

围绕对象旋转和移动相机不起作用Swift SceneKit

当 .searchable 修饰符处于活动状态时,如何将变量设置为 false?

如何在 SwiftUI 中通过通用 @ObservedObject 进行切换?

React Native IOS base64 编码图像不显示

在 SwiftUI 中放置全局 NavigationPath 的位置

如何在 SwiftUI 中创建显示当前电池电量的电池徽章

如何为视图的 colored颜色 和宽度设置动画?

无法为 Flutter 项目构建 ipa [CocoaPods 找不到 pod "GoogleAppMeasurement" 的兼容版本:]

使用 CIImage 支持的 UIImage 设置 UIImageView 时发生罕见的崩溃

如何在 Swift 中旋转 UIButton 和 UILabel 的文本?

iOS - 确保在主线程上执行

水平滚动 UICollectionView 时对齐单元格的中心

是否可以将 Socket.io 与 AWS Lambda 一起使用?