Language/Swift

SwiftUI를 이용한 조종바 구현하기(Slider)

IFLA 2022. 11. 16. 09:00

유튜브에서 소리조절 버튼을 클릭을 하면 소리를 조절할 수 있는 화면이 나타난다. 그럴때 사용하는 조종바를 SwiftUI에서는 Slider를 이용해 구현한다.

 

기본 코드

struct ContentView: View {
    @State private var sliderValue = 0.0

    var body: some View {
        VStack {
            Slider(value: $sliderValue, in: -100..100, step: 1)
            Text("\(sliderValue)")
        }
    }
}
  • value : Slider로 값을 조정할 때 값을 담는 변수다.
  • in : 슬라이더의 범위
  • step : 슬라이더를 이동할 때 값을 변경하는 정도다.

 

실행 화면

 

슬라이더의 색상 추가하기

Slider(value: $sliderValue, in: 1..100, step: 1)
    .accentColor(.yellow)

 


개발자 Document

Apple Developer Documentation

 

Apple Developer Documentation

 

developer.apple.com