특정 시간에만 지정 가능한 TimePicker 만들기

3605 단어 SwiftSwiftUI
완성형은 이런 느낌


이번에는 00과 30만 지정 가능한 TimePicker를 만듭니다.
minuteInterval을 지정하는 것으로 선택을 제한할 수 있습니다만 UX가 나쁘기 때문에 이쪽이 추천입니다
설명하지 않기 때문에 코드 만 올려 둡니다.

UIKit


class ViewController: UIViewController {
    let timePicker: UIDatePicker = {
        let dp = UIDatePicker()
        dp.datePickerMode = UIDatePicker.Mode.time
        dp.timeZone = NSTimeZone.local
        dp.preferredDatePickerStyle = .wheels
        dp.locale = Locale.init(identifier: "Japanese")
        dp.addTarget(self, action: #selector(timeChange), for: .valueChanged)
        return dp
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        timeChange()

        timePicker.backgroundColor = .white
        timePicker.tintColor = UIColor.black
        timePicker.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(timePicker)
        timePicker.clipsToBounds = true
        timePicker.layer.cornerRadius = 10.0
    }

    @objc func timeChange() {
        let minutes = DateFormatter()
        minutes.timeZone = .current
        minutes.timeStyle = .short
        minutes.dateStyle = .none
        minutes.locale = NSLocale.system
        minutes.dateFormat = "m"

        let SelectTime = Calendar.current.date(byAdding: .hour, value: -3, to: timePicker.date)!
        let Minutes = Int(minutes.string(from: SelectTime))!
        if 0 < Minutes && Minutes < 30 {
            let modifiedDate = Calendar.current.date(byAdding: .minute, value: 30 - Minutes, to: timePicker.date)!
            timePicker.date = modifiedDate
        }
        if 30 < Minutes && Minutes < 60 {
            let modifiedDate = Calendar.current.date(byAdding: .minute, value: 60 - Minutes, to: timePicker.date)!
            timePicker.date = modifiedDate
        }
    }
}

SwiftUI


struct ContentView: View {
    @State private var TIME = Date()
    var body: some View {
        VStack {
            DatePicker("", selection: $TIME, displayedComponents: .hourAndMinute)
                .frame(width: 300, height: 60)
                .datePickerStyle(WheelDatePickerStyle())
                .labelsHidden()
                .cornerRadius(10)
                .onChange(of: TIME) { i in
                    ChangeTime()
                }
        }
        .onAppear() {
            ChangeTime()
        }
    }
    func timeM() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = .current
        dateFormatter.timeStyle = .short
        dateFormatter.dateStyle = .none
        dateFormatter.locale = NSLocale.system
        dateFormatter.dateFormat = "m"
        let mString = dateFormatter.string(from: TIME)
        return mString
    }
    func ChangeTime() {
        let mString = timeM()
        let m = Int(mString)!
        if 0 < m && m < 30 {
            let NextTime = Calendar.current.date(byAdding: .minute, value: 30 - m, to: TIME)!
            TIME = NextTime
            print(TIME)
        } else {
            if 30 < m && m < 60 {
                let NextTime = Calendar.current.date(byAdding: .minute, value: 60 - m, to: TIME)!
                TIME = NextTime
                print(TIME)
            }
        }
    }
}

UIKit은 전혀 이해하지 못하기 때문에 재미있는 부분이있을 수 있습니다.
내가 생각했던대로 움직이고 있기 때문에 좋다.

좋은 웹페이지 즐겨찾기