Upgrade to Pro — share decks privately, control downloads, hide ads and more …

UIControlとUIKeyInputでUIPickerViewを 手軽に下からシュッと出す

kazuhiro4949
May 25, 2017
490

UIControlとUIKeyInputでUIPickerViewを 手軽に下からシュッと出す

kazuhiro4949

May 25, 2017
Tweet

More Decks by kazuhiro4949

Transcript

  1. 6*,FZ*OQVUΛܧঝ͢Δ 6*,FZ*OQVUΛܧঝͨ͠ΫϥεΛ࢖͏ͱࣗ༝ʹιϑτ΢ΣΞ ΩʔϘʔυ͕දࣔͤ͞ΒΕ·͢ extension PickerKeyboard: UIKeyInput { var hasText: Bool

    { return !textStore.isEmpty } func insertText(_ text: String) { textStore += text setNeedsDisplay() } func deleteBackward() { textStore.remove( at: textStore.characters.index( before: textStore.characters.endIndex)) setNeedsDisplay() } }
  2. 6*,FZ*OQVUΛܧঝ͢Δ 6*,FZ*OQVUΛܧঝͨ͠ΫϥεΛ࢖͏ͱࣗ༝ʹιϑτ΢ΣΞ ΩʔϘʔυ͕දࣔͤ͞ΒΕ·͢ extension PickerKeyboard: UIKeyInput { var hasText: Bool

    { return !textStore.isEmpty } func insertText(_ text: String) { textStore += text setNeedsDisplay() } func deleteBackward() { textStore.remove( at: textStore.characters.index( before: textStore.characters.endIndex)) setNeedsDisplay() } } ͜ͷ̏ͭΛ࣮૷͢Ε͹େৎ෉
  3. 6*,FZ*OQVUΛܧঝ͢Δ 6*,FZ*OQVUΛܧঝͨ͠ΫϥεΛ࢖͏ͱࣗ༝ʹιϑτ΢ΣΞ ΩʔϘʔυ͕දࣔͤ͞ΒΕ·͢ extension PickerKeyboard: UIKeyInput { var hasText: Bool

    { return !textStore.isEmpty } func insertText(_ text: String) { textStore += text setNeedsDisplay() } func deleteBackward() { textStore.remove( at: textStore.characters.index( before: textStore.characters.endIndex)) setNeedsDisplay() } }
  4. λοϓͰ7JFXΛ'JSTU3FTQPOEFSʹ͢Δ ઌఔͷΫϥε͕'JSTU3FTQPOEFSʹͳΔͱιϑτ΢ΣΞΩʔ Ϙʔυ͕ग़͖ͯ·͢ class PickerKeyboard: UIControl { required init?(coder aDecoder:

    NSCoder) { super.init(coder: aDecoder) addTarget(self, action: #selector(PickerKeyboard.didTap(sender:)), for: .touchUpInside) } func didTap(sender: PickerKeyboard) { becomeFirstResponder() } override var canBecomeFirstResponder: Bool { return true } }
  5. λοϓͰ7JFXΛ'JSTU3FTQPOEFSʹ͢Δ ઌఔͷΫϥε͕'JSTU3FTQPOEFSʹͳΔͱιϑτ΢ΣΞΩʔ Ϙʔυ͕ग़͖ͯ·͢ class PickerKeyboard: UIControl { required init?(coder aDecoder:

    NSCoder) { super.init(coder: aDecoder) addTarget(self, action: #selector(PickerKeyboard.didTap(sender:)), for: .touchUpInside) } func didTap(sender: PickerKeyboard) { becomeFirstResponder() } override var canBecomeFirstResponder: Bool { return true } }
  6. λοϓͰ7JFXΛ'JSTU3FTQPOEFSʹ͢Δ ઌఔͷΫϥε͕'JSTU3FTQPOEFSʹͳΔͱιϑτ΢ΣΞΩʔ Ϙʔυ͕ग़͖ͯ·͢ class PickerKeyboard: UIControl { required init?(coder aDecoder:

    NSCoder) { super.init(coder: aDecoder) addTarget(self, action: #selector(PickerKeyboard.didTap(sender:)), for: .touchUpInside) } func didTap(sender: PickerKeyboard) { becomeFirstResponder() } override var canBecomeFirstResponder: Bool { return true } }
  7. λοϓͰ7JFXΛ'JSTU3FTQPOEFSʹ͢Δ ઌఔͷΫϥε͕'JSTU3FTQPOEFSʹͳΔͱιϑτ΢ΣΞΩʔ Ϙʔυ͕ग़͖ͯ·͢ class PickerKeyboard: UIControl { required init?(coder aDecoder:

    NSCoder) { super.init(coder: aDecoder) addTarget(self, action: #selector(PickerKeyboard.didTap(sender:)), for: .touchUpInside) } func didTap(sender: PickerKeyboard) { becomeFirstResponder() } override var canBecomeFirstResponder: Bool { return true } }
  8. 6*3FTQPOEFSͷJOQVU7JFX ͱͯ͠6*1JDLFS7JFXΛฦ͢ JOQVU7JFX͕ฦ͢7JFX͸ɺιϑτ΢ΣΞΩʔϘʔυͷ୅ΘΓ ͱͯ͠දࣔ͞Ε·͢ class PickerKeyboard: UIControl { override var

    inputView: UIView? { let pickerView = UIPickerView() pickerView.delegate = self let row = data.index(of: textStore) ?? -1 pickerView.selectRow(row, inComponent: 0, animated: false) return pickerView } }
  9. 6*3FTQPOEFSͷJOQVU7JFX ͱͯ͠6*1JDLFS7JFXΛฦ͢ JOQVU7JFX͕ฦ͢7JFX͸ɺιϑτ΢ΣΞΩʔϘʔυͷ୅ΘΓ ͱͯ͠දࣔ͞Ε·͢ class PickerKeyboard: UIControl { override var

    inputView: UIView? { let pickerView = UIPickerView() pickerView.delegate = self let row = data.index(of: textStore) ?? -1 pickerView.selectRow(row, inComponent: 0, animated: false) return pickerView } }
  10. 6*3FTQPOEFSͷJOQVU7JFX ͱͯ͠6*1JDLFS7JFXΛฦ͢ JOQVU7JFX͕ฦ͢7JFX͸ɺιϑτ΢ΣΞΩʔϘʔυͷ୅ΘΓ ͱͯ͠දࣔ͞Ε·͢ class PickerKeyboard: UIControl { override var

    inputView: UIView? { let pickerView = UIPickerView() pickerView.delegate = self let row = data.index(of: textStore) ?? -1 pickerView.selectRow(row, inComponent: 0, animated: false) return pickerView } }
  11. 6*1JDLFS7JFXͷ %FMFHBUF %BUB4PVSDFΛ࣮૷ class PickerKeyboard: UIControl { var data: [String]

    = ["Mon.", "Tue.", "Wed.", "Thu.", "Fri.", "Sut.", "Sun."] // .. } extension PickerKeyboard: UIPickerViewDelegate, UIPickerViewDataSource { func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return data.count } func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return data[row] } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { textStore = data[row] setNeedsDisplay() } }
  12. 6*1JDLFS7JFXͷ %FMFHBUF %BUB4PVSDFΛ࣮૷ class PickerKeyboard: UIControl { var data: [String]

    = ["Mon.", "Tue.", "Wed.", "Thu.", "Fri.", "Sut.", "Sun."] // .. } extension PickerKeyboard: UIPickerViewDelegate, UIPickerViewDataSource { func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return data.count } func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return data[row] } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { textStore = data[row] setNeedsDisplay() } } આ໌লུ ཁ͢Δʹ6*1JDLFS7JFXΛී௨ʹ࣮૷͍ͯͩ͘͠͞
  13. JOQVU"DDFTTPSZ7JFXʹด͡ΔϘλ ϯΛ഑ஔ͢Δ 6*3FTQPOEFSͷJOQVU"DDFTTPSZ7JFX͸ ΩʔϘʔυͷ্ʹ͍ͬͭͯ͘͘Δ೚ҙͷ7JFXΛฦͤ·͢ class PickerKeyboard: UIControl { // ..

    func didTapDone(sender: UIButton) { resignFirstResponder() } override var inputAccessoryView: UIView? { let button = UIButton(type: .system) // .. button.addTarget(self, action: #selector(PickerKeyboard.didTap(sender:)), for: .touchUpInside) let view = UIView(frame: CGRect(x: 0, y: 0, width: self.bounds.width, height: 44)) // .. view.addSubview(button) return view } }
  14. JOQVU"DDFTTPSZ7JFXʹด͡ΔϘλ ϯΛ഑ஔ͢Δ 6*3FTQPOEFSͷJOQVU"DDFTTPSZ7JFX͸ ΩʔϘʔυͷ্ʹ͍ͬͭͯ͘͘Δ೚ҙͷ7JFXΛฦͤ·͢ class PickerKeyboard: UIControl { // ..

    func didTapDone(sender: UIButton) { resignFirstResponder() } override var inputAccessoryView: UIView? { let button = UIButton(type: .system) // .. button.addTarget(self, action: #selector(PickerKeyboard.didTap(sender:)), for: .touchUpInside) let view = UIView(frame: CGRect(x: 0, y: 0, width: self.bounds.width, height: 44)) // .. view.addSubview(button) return view } }
  15. JOQVU"DDFTTPSZ7JFXʹด͡ΔϘλ ϯΛ഑ஔ͢Δ 6*3FTQPOEFSͷJOQVU"DDFTTPSZ7JFX͸ ΩʔϘʔυͷ্ʹ͍ͬͭͯ͘͘Δ೚ҙͷ7JFXΛฦͤ·͢ class PickerKeyboard: UIControl { // ..

    func didTapDone(sender: UIButton) { resignFirstResponder() } override var inputAccessoryView: UIView? { let button = UIButton(type: .system) // .. button.addTarget(self, action: #selector(PickerKeyboard.didTap(sender:)), for: .touchUpInside) let view = UIView(frame: CGRect(x: 0, y: 0, width: self.bounds.width, height: 44)) // .. view.addSubview(button) return view } }