如何从Swift代码中更改SwiftUI的Slider值

rryofs0p  于 2023-04-28  发布在  Swift
关注(0)|答案(1)|浏览(122)

我有两个已发布的属性,名为sliderMinValuesliderMaxValue。这些值在一个名为GameViewController的类中变化。
GameViewController:

import Cocoa

class GameViewController: NSObject, ObservableObject {
        
    @Published var sliderMinValue: Double = 0.0
    @Published var sliderMaxValue: Double = 1.0
    
    ...
    
    sliderMinValue and sliderMaxValue are changed in this class...
    
    ...
    
}

我可以通过下面的GameView将这两个值传递给SwiftUI Slider。
GameView:

import SwiftUI

struct GameView: View {

@ObservedObject var gameViewController = GameViewController()

var body: some View {
        
        HStack {
            
            ZStack {
                
                GoBan19View()
                StoneMapView()
            }
            
            SliderView()
            
        }.environmentObject(gameViewController)
    }
}

这是我的SliderView。
SliderView:

import SwiftUI

struct SliderView: View {

    @EnvironmentObject var gameViewController: GameViewController

    @State private var sliderVal: Double = 0
    @State var changing = false
        
    var body: some View {
        VStack {
            
            // For debug
            Text(String(gameViewController.sliderMinValue))
            Text(String(gameViewController.sliderMaxValue))
            
            Slider(value: $sliderVal,
                   in: gameViewController.sliderMinValue...gameViewController.sliderMaxValue,
                   step: 1,
                   onEditingChanged: { change in
                                        
                        self.changing = change
                
            })
            .padding()
            Text("CurrentValue:\(sliderVal)")
            Text(changing ? "Changing" : "Stopping")
        }
    }
}

我的问题是sliiderVal是否可以通过swift代码进行更改。如果可以,我想将sliiderVal设置为gameViewController.sliderMaxValue。在StoryBoard的滑块中,我可以像代码self.horizontalSlider.floatValue = Float(self.horizontalSlider.maxValue)那样设置滑块值

qc6wkl3g

qc6wkl3g1#

下面是一些示例代码,展示了如何使用GameViewController构造observe滑块中的值。这也意味着您可以在代码中的任何位置更改该值(sliderValue),它将反映在Slider中。

import Foundation
 import SwiftUI

class GameViewController: NSObject, ObservableObject {
    @Published var sliderMinValue: Double = 0.0
    @Published var sliderMaxValue: Double = 123.0  // <-- for testing
    
    @Published var sliderValue: Double = 0.0  // <--- here
}

struct ContentView: View {
    var body: some View {
        GameView()
    }
}

struct GameView: View {
    
    @StateObject var gameViewController = GameViewController()  // <--- here
    
    var body: some View {
        HStack {
            //            ZStack {
            //                GoBan19View()
            //                StoneMapView()
            //            }
            SliderView()
        }.environmentObject(gameViewController)
    }
}

struct SliderView: View {
    @EnvironmentObject var gameViewController: GameViewController
    @State var changing = false
    
    var body: some View {
        VStack {
            Text(String(gameViewController.sliderMinValue))
            Text(String(gameViewController.sliderMaxValue))
            Text("\(gameViewController.sliderValue)") // <--- here
            
            Slider(value: $gameViewController.sliderValue,  // <--- here
                   in: gameViewController.sliderMinValue...gameViewController.sliderMaxValue,
                   step: 1,
                   onEditingChanged: { change in
                self.changing = change
            })
            .padding()
            Text("CurrentValue:\(gameViewController.sliderValue)") // <--- here
            Text(changing ? "Changing" : "Stopping")
        }
    }
}

相关问题