Swift

[Swift] UIPickerViewで秒まで設定できるViewを作る

More than 1 year has passed since last update.

[Swift] UIPickerViewで秒まで設定できるViewを作る

class TimerPickerView: UIPickerView, UIPickerViewDelegate, UIPickerViewDataSource {

    let dataList = [[Int](0...24), [Int](0...60), [Int](0...60)]
    var hour = 0
    var minute = 0
    var second = 0
    var date = Date()

    override init(frame: CGRect) {
        super.init(frame:frame)
        self.delegate = self
        self.dataSource = self
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
        self.delegate = self
        self.dataSource = self
    }

    override func didAddSubview(_ subview: UIView) {
        super.didAddSubview(subview)
        //ラインの色を変える
        let label = UILabel()
        label.text = ":"
        label.sizeToFit()
        //自作のラベルより小さいの場合はラインなので色を変える
        if subview.bounds.height < label.bounds.height {
            subview.backgroundColor = .blue
        }
    }

    func setLabels() {
        //「:」のラベルを追加する
        let hourLabel = UILabel()
        hourLabel.text = ":"//"hour"
        hourLabel.backgroundColor = UIColor.clear
        hourLabel.textColor = Settings.shared.color_.label()
        hourLabel.sizeToFit()
        hourLabel.frame = CGRect(x: self.bounds.width / 3 * 1,
                                 y: self.bounds.height / 2 - (hourLabel.bounds.height / 2),
                                 width: hourLabel.bounds.width,height:  hourLabel.bounds.height)
        self.addSubview(hourLabel)

        let minLabel = UILabel()
        minLabel.text = ":"//"min"
        minLabel.backgroundColor = UIColor.clear
        minLabel.textColor = Settings.shared.color_.label()
        minLabel.sizeToFit()
        minLabel.frame = CGRect(x: self.bounds.width / 3 * 2,
                                y: self.bounds.height / 2 - (minLabel.bounds.height / 2),
                                width: minLabel.bounds.width,height:  minLabel.bounds.height)
        self.addSubview(minLabel)
    }

    public override func layoutSubviews() {
        super.layoutSubviews()
        self.setLabels()
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return dataList.count
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return dataList[component].count
    }

    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        return NSAttributedString(string: String(format: "%02d", dataList[component][row]), 
                                  attributes: [NSForegroundColorAttributeName: Settings.shared.color_.label()])
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return String(format: "%02d", dataList[component][row])
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        switch component {
        case 0://hour
            hour = dataList[component][row]
        case 1://min
            minute = dataList[component][row]
        case 2://sec
            second = dataList[component][row]
        default: break
        }
        updateDate()
    }

    func pickerHour(_ value: Int) {
        hour = value
        selectRow(value, inComponent: 0, animated: true)
        updateDate()
    }

    func pickerMinute(_ value: Int) {
        minute = value
        selectRow(value, inComponent: 1, animated: true)
        updateDate()
    }

    func pickerSecond(_ value: Int) {
        second = value
        selectRow(value, inComponent: 2, animated: true)
        updateDate()
    }

    func updateDate() {
        let calendar: Calendar = Calendar(identifier: Calendar.Identifier.gregorian)
        var component = (calendar as NSCalendar).components([.hour, .minute, .second], from: date)
        component.hour = hour
        component.minute = minute
        component.second = second
        date = calendar.date(from: component)!
    }
}