• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

SwiftUI 中Slider的使用

开发技术 开发技术 3小时前 1次浏览

效果如下
SwiftUI 中Slider的使用

import SwiftUI

struct ContentView: View {
   
    @State private var value = 0.0
    @State private var isEditing = false
    @State private var value1:Float = 0.0
    
    var body: some View {
        VStack {
            Text("滑块的值:(value,specifier:"%.2f")")
                .foregroundColor(isEditing ? .red : .black)
            Slider(value: $value, in: 0...100, step: 1, onEditingChanged: { (isEditing) in
                self.isEditing = isEditing
            }, minimumValueLabel: Text("1"), maximumValueLabel: Text("100")) {
                Text("11")
            }
            .accentColor(.green)
            .border(Color.red)
            VolumnChangeSlider(percentage: $value1)
                .frame(height: 40)
                .padding(.horizontal)
        }
    }
}

struct VolumnChangeSlider: View{
    @Binding var percentage: Float
    var body: some View{
        GeometryReader { geometry in
            ZStack(alignment: .leading){
                Rectangle()
                    .foregroundColor(.gray)
                Rectangle()
                    .foregroundColor(.blue)
                    .frame(width: geometry.size.width * CGFloat(self.percentage / 100))
            }
            .cornerRadius(12)
            .gesture(DragGesture(minimumDistance: 0).onChanged({ val in
                self.percentage = min(max(0, Float(val.location.x/geometry.size.width*100)), 100)
            }))
        }
    }
    
}


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

程序员灯塔
转载请注明原文链接:SwiftUI 中Slider的使用
喜欢 (0)