SwiftUI @状态变量

时间:2019-06-28 02:49:06

标签: swiftui

*我有以下三个@State变量:

@State var mhzValue : Float = 0 
@State var mhzValueStep : Float = 0 
@State var TotalFrequency : Float = 0 

尽管它们在我的应用程序中显示为mhzValue和mhzValueStep。

我希望将它们添加在一起。

示例:var TotalFrequency = mhzValue + mhzValueStep

但是我只是无法使它工作。

请提出任何建议。.我对此很陌生...谢谢!*

import SwiftUI
struct DipoleView : View {

    @State var mhzValue : Float = 0
    @State var mhzValueStep : Float = 0
    @State var TotalFrequency : Float = 0

    var body: some View {
        VStack {
            //Slider one
            Text("Slide to select Frequency")
                .font(.headline)
                .color(.blue)
                .padding(.leading, -130.0)
            Slider(value: $mhzValue, from: 1, through: 55, by: 1)
                .padding(.horizontal)

            Text("\(Int(mhzValue)) in Mhz")
                .font(.title)
                .fontWeight(.semibold)
                .color(.blue)
            // Slider Two
            Text("Slide to select Decimal Point")
                .font(.headline)
                .color(.orange)
                .padding(.leading, -130.0)

            Slider(value: $mhzValueStep, from: 1, through: 999, by: 0.1)
                .padding(.horizontal)
            Text(".\(Int(mhzValueStep)) in Mhz")
                .font(.title)
                .fontWeight(.semibold)
                .color(.orange)
            Text(" Frequency:  \(Int(mhzValue)).\(Int(mhzValueStep)) Mhz")
                .font(.largeTitle)
                .fontWeight(.medium)
                .color(.white)
                .padding(10)
                .background(/*@START_MENU_TOKEN@*/Color.blue/*@END_MENU_TOKEN@*/)
                .cornerRadius(10.0)
                .shadow(radius: /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/)

            // Load Image View
            Spacer()
            ImageView()
                .padding(.bottom, 40)

        }
    }
}

#if DEBUG
struct DipoleView_Previews : PreviewProvider {
    static var previews: some View {
        DipoleView()
    }
}
#endif

1 个答案:

答案 0 :(得分:2)

您无需将TotalFrequency声明为State变量,因为mhzValue和mhzValueStep都被声明为State var,而您只需要这两者的总和即可。 而是可以将TotalFrequency声明为计算的var。这是该代码的有效版本。

import SwiftUI
struct ContentView : View {

    @State var mhzValue : Float = 0
    @State var mhzValueStep : Float = 0
    private var TotalFrequency : Float {
        return mhzValue + mhzValueStep
    }

    var body: some View {
        VStack {
            //Slider one
            Text("Slide to select Frequency")
                .font(.headline)
                .color(.blue)
                .padding(.leading, -130.0)
            Slider(value: $mhzValue, from: 1, through: 55, by: 1)
                .padding(.horizontal)

            Text("\(Int(mhzValue)) in Mhz")
                .font(.title)
                .fontWeight(.semibold)
                .color(.blue)
            // Slider Two
            Text("Slide to select Decimal Point")
                .font(.headline)
                .color(.orange)
                .padding(.leading, -130.0)

            Slider(value: $mhzValueStep, from: 0, through: 1, by: 0.1)
                .padding(.horizontal)
            Text("\(mhzValueStep) in Mhz")
                .font(.title)
                .fontWeight(.semibold)
                .color(.orange)

            Text(" Frequency:  \(TotalFrequency) Mhz")
                .font(.largeTitle)
                .fontWeight(.medium)
                .color(.white)
                .padding(10)
                .background(/*@START_MENU_TOKEN@*/Color.blue/*@END_MENU_TOKEN@*/)
                .cornerRadius(10.0)
                .shadow(radius: /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/)

            // Load Image View
            Spacer()
        //    Image()
       //         .padding(.bottom, 40)

        }
    }
}