반응형

 

Hacking with Swift 사이트의 강좌 번역본입니다.

 

[원문 : https://www.hackingwithswift.com/quick-start/swiftui/how-to-create-a-slider-and-read-values-from-it]

 

How to create a slider and read values from it

[동영상 강좌 : https://youtu.be/EFkL0Jv58wQ]

SwiftUI의 Slider 뷰는 값을 저장할수 있는 곳에 바인딩이 필요하지만, UISlider과 매우 비슷합니다.

 

Slider를 만들때 다양한 매개변수를 제공할 수 있지만, 가장 관심이 있는 매개변수는 다음과 같습니다.

  • Value: Double로 바인딩하는 것
  • In: slider의 범위
  • Step: slider를 이동할때 값을 얼마만큼 변경하는지

예를들어, 다음 코드는 Celsius 프로퍼티에 바인딩하는 slider를 만들고나서 slider를 움직일때 텍스트 뷰를 업데이트 하므로써 섭씨(Celsius)와 화씨(Fahrenheit)간에 변환합니다. 

struct ContentView: View {
    @State private var celsius: Double = 0

    var body: some View {
        VStack {
            Slider(value: $celsius, in: -100...100, step: 0.1)
            Text("\(celsius) Celsius is \(celsius * 9 / 5 + 32) Fahrenheit")
        }
    }
}

 

반응형
Posted by 까칠코더
,