【Xcode/Swift】RxSwiftでスライダーの値をリアルタイムで検知する

Contents 非表示

実装

View

ViewController

import UIKit
import RxSwift
import RxCocoa

final class HomeViewController: UIViewController {
    // MARK: - Properties
    @IBOutlet private weak var sliderValueLabel: UILabel!
    @IBOutlet private weak var bottomSlider: UISlider!

    private let disposeBag = DisposeBag()

    // MARK: - View Life Cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        bind()
    }

}

// MARK: - Binding
private extension HomeViewController {
    func bind() {
        bottomSlider.rx.value
            .map { value in
                return String(format: "Percent: %.0f %%", value * 100)
            }
            .bind(to: sliderValueLabel.rx.text)
            .disposed(by: disposeBag)
    }
}