SwiftUI 中Stepper的使用

效果如下

import SwiftUI

struct ContentView: View {
    @State private var selectIndex = 0
    let color: [Color] = [.orange,.red,.gray,.blue,.green,.purple,.pink]
    
    @State private var value = 0
    @State private var circleSize :CGFloat = 20
    let step = 5
    let range = 0...50
    var body: some View {
       
        
        VStack {
            Stepper(value: $value, in: range, step: step) { (isEditable) in
                print("soso (isEditable)")
            } label: {
                Text("当前值为:(value)")
            }
            
            Stepper(onIncrement: increment, onDecrement: decrement) {
                Text("array index = (selectIndex) color:(color[selectIndex].description)")
            }.background(color[selectIndex])
            
            Stepper("",value: $circleSize,in: 10...500,step: 10)
                .labelsHidden()
            Circle()
                .frame( circleSize, height: circleSize)
                .foregroundColor(Color.blue)
                .animation(.easeInOut)
        }

        
    }
    private func increment(){
        selectIndex += 1
        if selectIndex >= color.count{selectIndex = 0}
    }
    private func decrement(){
        selectIndex -= 1
        if selectIndex <= 0{selectIndex = color.count-1}
    }
}



struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
原文地址:https://www.cnblogs.com/chaostudy/p/15034798.html