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

Responder Chainを使って コードをスッキリさせたい - 第1回 HAKATA.swift

ukitaka
April 16, 2018

Responder Chainを使って コードをスッキリさせたい - 第1回 HAKATA.swift

ukitaka

April 16, 2018
Tweet

More Decks by ukitaka

Other Decks in Programming

Transcript

  1. Responder ChainΛ࢖ͬͯ
    ίʔυΛεοΩϦ͍ͤͨ͞
    HAKATA.swift
    @ukitaka

    View full-size slide

  2. twitter: @ukitaka_
    github: @ukitaka
    Yuki Takahashi

    View full-size slide

  3. ࣗݾ঺հ
    ❤ ؔ਺ܕϓϩάϥϛϯάɺDDD
    ✏ ܕγεςϜɺίϯύΠϥ
    ௿ϨΠϠʔ

    View full-size slide

  4. ͍͖ͳΓͰ͕͢ɺ
    ͜ΜͳέʔεΛߟ͑ͯΈΔ

    View full-size slide

  5. Cellͷதͷ
    ϘλϯΛλοϓͨ͠Β
    Alert / ActionSheetग़͍ͨ͠

    View full-size slide

  6. ஍ຯʹΊΜͲ͍͘͞
    • UIAlertControllerΛग़͢ʹ͸ViewControllerͰ
    ॲཧΛ͢Δඞཁ͕͋Δ
    • Cell͸ViewControllerͳΜͯ஌Βͳ͍
    • ViewController΋CellͷϘλϯΛ(ී௨͸)௚઀ࢀ
    র͍ͯ͠ͳ͍

    View full-size slide

  7. Α͋͘Δରॲ๏
    • DelegateΛҾճ͢
    • ֊૚͕ਂ͘ͳΕ͹ͳΔ΄Ͳਏ͍
    • Viewߏ଄ͷมԽʹऑ͍

    View full-size slide

  8. ͳΜͱ͔͍ͨ͠

    View full-size slide

  9. ݪ఺ʹཱͪฦͬͯMVC

    View full-size slide

  10. MVCͰ͸Ͳ͏͋Δ΂͖ʁ

    View full-size slide

  11. ϢʔβΞΫγϣϯͷॲཧ
    • MVCʹ͓͚ΔControllerͷΑ͏ͳΦϒδΣΫ
    τʹ௨஌Λͯ͠ϢʔβΞΫγϣϯड͚ͱΔͷ
    ͕ී௨
    • MVCʹݶΒͣɺͲͷΞʔΩςΫνϟͰ͋ͬͯ
    ΋ಉ͡

    View full-size slide

  12. ௨஌Λ͠Α͏

    View full-size slide

  13. ௨஌͢ΔଆΠϝʔδ
    .notifyΛݺͿ͚ͩ

    View full-size slide

  14. ௨஌Λड͚औΔଆΠϝʔδ
    ͍͍ײ͡ʹDelegateͰड͚औΕΔ

    View full-size slide

  15. DelegateΛҾճ͢ඞཁ͕ͳ͍
    DelegateύλʔϯͷΑ͏ͳ
    Πϝʔδ

    View full-size slide

  16. ͦ͜Ͱ
    Responder Chain

    View full-size slide

  17. = UIResponderͷ࿈ͳΓ

    View full-size slide

  18. UIResponder
    • UIReponder = ϢʔβͷೖྗΛर͑Δ΍ͭ
    • ͋ΔsubviewͰΠϕϯτΛर͏ͱॲཧͰ͖Δͱ
    ͜Ζ·Ͱ࣍ʑʹॲཧΛճ͍ͯ͘͠
    • self.next Ͱ࣍ͷResponder͕औΕΔ
    = Responder Chain

    View full-size slide

  19. UIResponder
    • ResponderChain্ʹ͸౰વViewController΋
    ͍Δʂ
    • DelegateΛҾ͖ճ͢·Ͱ΋ͳ͘ɺ͢Ͱʹͭͳ
    ͕͍ͬͯΔ
    • ͜Εʹ৐͔ͬΕ͹͍͍ײ͡ʹViewControllerʹ
    ʮ௨஌ʯͰ͖ͦ͏ʂ

    View full-size slide

  20. ͳΜ͔࢖͑ͦ͏

    View full-size slide

  21. Ͳ͏΍Δ͔ʁ

    View full-size slide

  22. ActionΛఆٛ͢Δ

    View full-size slide

  23. DelegateΛఆٛ͢Δ

    View full-size slide

  24. ViewEventΛఆٛ͢Δ
    ୭͕ϋϯυϧ͢Δ͔ͱɺ
    Ͳ͏σΟεύον͢Δ͔ΛܾΊΔɻ

    View full-size slide

  25. ViewEventΛఆٛ͢Δ

    View full-size slide

  26. UIResponderΛ֦ு͢Δ
    Responder ChainΛḷͬͯɺ
    ΠϕϯτΛϋϯυϧͰ͖Δ΍ͭΛݟ͚ͭΔ

    View full-size slide

  27. ௨஌͢ΔଆΠϝʔδ
    .notifyΛݺͿ͚ͩ

    View full-size slide

  28. ௨஌Λड͚औΔଆΠϝʔδ
    ͍͍ײ͡ʹDelegateͰड͚औΕΔ

    View full-size slide

  29. ཧ૝ͷੈք͕࣮ݱͨ͠

    View full-size slide

  30. ྑ͍఺
    • DelegateΛViewͷਂ͍֊૚·ͰҾճ͢ඞཁ͕
    ͳ͘ͳͬͨ
    • Viewͷ֊૚͕มԽͨ͠ͱͯ͠΋ɺίʔυͷม
    ߋ͕ෆཁɻViewͷมߋʹڧ͍ɻ

    View full-size slide

  31. มΘΒͳ͍఺
    • ͋ΔΠϕϯτΛड͚औΔͨΊͷprotocol࣮૷
    ͠๨ΕΛίϯύΠϧ࣌ʹ͸ݕग़Ͱ͖ͳ͍
    • ͜Ε͸Delegateઃఆ͠๨ΕͰ΋ಉ͡
    • ࣮ࡍʹಈ͔͢ or ςετͰΧόʔ

    View full-size slide

  32. RxCocoa΁ͷԠ༻

    View full-size slide

  33. RxCocoa + Responder Chain
    • RxΛ࢖͍ͬͯͯ΋ಉ͡໰୊͕ى͜Δ
    • RxDataSourcesΛ࢖͍ͬͯͯ΋ɺCellͷதͷΠ
    ϕϯτ͸र͑ͳ͍
    • Responder Chainʹྲྀͯ͠͠·͓͏

    View full-size slide

  34. ukitaka/RxResponderChain

    View full-size slide

  35. ࡶʹResponder Chainʹྲྀ͢
    Ϙλϯ͕λοϓ͞ΕͨΒ

    View full-size slide

  36. ࡶʹResponder Chainʹྲྀ͢
    ద੾ͳܕʹม׵ͯ͠

    View full-size slide

  37. ࡶʹResponder Chainʹྲྀ͢
    Responder Chainʹྲྀ͢

    View full-size slide

  38. ࡶʹVCͰड͚औΔ
    ViewControllerͰ
    ܕΛࢦఆͯ͠ड͚औΔ

    View full-size slide

  39. গ͚࣮ͩ͠૷ΛΈΔ

    View full-size slide

  40. ࣮૷ͷཪଆ
    ࠇຐज़ʹΑ֤ͬͯUIResponderΦϒδΣΫτʹ
    PublishSubjectΛ࣋ͨͤΔ

    View full-size slide

  41. ࣮૷ͷཪଆ
    self.rx.responderChain Λੜ΍͢

    View full-size slide

  42. ࣮૷ͷཪଆ
    ͜ͷΦϒδΣΫτ͸ObserverType

    View full-size slide

  43. ࣮૷ͷཪଆ
    ઒ʹEvent͕ྲྀΕ͖ͯͨΒ
    ResponderChainΛḷͬͯ

    View full-size slide

  44. ࣮૷ͷཪଆ
    ͖ͬ͞ੜ΍ͨ͠PublishSubjectʹ
    ΠϕϯτΛྲྀ͢

    View full-size slide

  45. ࣮૷ͷཪଆ
    ड͚औΔͱ͖͸ܕͰfilter

    View full-size slide

  46. ResponderChain
    • View → ViewController͸Responder Chainܨ
    ͕͍ͬͯΔ
    • ௨஌ʹ࢖͑Δ / ָʹॻ͚Δ / มߋʹڧ͍

    View full-size slide