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

How to implement timetable view on iOS

How to implement timetable view on iOS

Hiroki Kumamoto

March 13, 2019
Tweet

More Decks by Hiroki Kumamoto

Other Decks in Programming

Transcript

  1. SpreavSheetView • kishikawakatsumi/SpreadsheetView • εϓϨουγʔτͬΆ͍UIΛ࡞ΕΔ • UITableView, UICollectionViewͱಉ͡Α͏ʹඳը͞Ε͍ͯΔՕॴͷ ΈදࣔͰ͖Δ •

    ηϧͷ݁߹ɾݻఆηϧʹ΋ରԠ • ηϧ͝ͱʹborderͷࢦఆ͕Ͱ͖Δ • ֦େ͸ඇαϙʔτ • ͜Ε͚ͩͰ͸ཁ݅Λຬͨͤͳ͍ແཧͬΆ͍
  2. λΠϜςʔϒϧͷجຊํ਑ • ཁ݅తʹ͸5෼ࠁΈͰεέδϡʔϧ͕૊·Ε͍ͯΔ • ྫ: εςʔδCͰ10:30~11:05 • ॎ࣠͸1෼୯Ґɺԣ࣠͸̍εςʔδ୯Ґ • ηϧΛ݁߹ͤͯ͞ɺλΠϜςʔϒϧΛ࡞Δ

    • ηϧͷ݁߹ϧʔϧ͸࠲ඪͷϖΞͰ((x1, y1), (x2,y2))දݱ͞ΕΔ • x1, y1, x2, y2 ͸̌Ҏ্ͷ੔਺ • 10:00ελʔτͱ͢Δͱ ((2, 30) (2, 65))Λ݁߹
  3. ζʔϜ΁ͷରԠ • ղܾҊ • ζʔϜ༻ͷUIScrollViewͰSpreadshhetViewΛϥοϓ • SpreadsheetView͸ηϧ͕શͯදࣔ͞ΕΔେ͖͞Λࢦఆ • ݻఆηϧ༻ͷUIScrollVIew΋΍͸ΓࣗલͰ࣋ͭ •

    ζʔϜͨ࣌͠ʹେ͖͞ͱεΫϩʔϧҐஔΛ௥ै • ຊମͷΈ͕֦େͯ͠ݻఆηϧ͸େ͖͞ͷΈେ͖͘ͳΔ • σϝϦοτ • λΠϜςʔϒϧͷαΠζ΍ηϧදࣔͷίετ͕Ͱ͔͘ͳΔͱύϑΥʔϚϯεʹ೉͋Γ • UITableViewɺUICollectionViewʢͲͪΒ΋ζʔϜʹରԠ͍ͯ͠ͳ͍ʣʹ΋ೖΕΔ͜ͱ͕ͩɺη ϧͷ஗ԆಡΈࠐΈͱζʔϜ͸૬ੑ͕ѱͦ͏
  4. ճస΁ͷରԠ • ճసલʹεΫϩʔϧҐஔΛ͓͍֮͑ͯͯճసޙʹ෮ݩ • ը໘ͷத৺࠲ඪ • ׂ߹Ͱ͓֮͑ͯ͘ • NOTES: UIScrollViewͷframe(autolayoutܦ༝)ͱcontentSize(αΠ

    ζʣͷมߋͱcontentOffset(εΫϩʔϧҐஔʣΛಉ࣌ʹߦ͏ͱ஋͕ ਖ਼͘͠൓ө͞Εͳ͍ͷͰɺ • frameͷมߋ->contentSizeͷมߋ-> contentOffsetͷมߋΛผ ͷRunLoopͰߦ͏
  5. BlockϦετͷܭࢉ(ٖࣅίʔυ) struct Block {
 var items: [Show]
 var start: Int


    var end: Int
 func hasOverlap(Show) -> Bool
 mutating func add(Show)
 mutating func merge(Block)
 }
 
 var blocks: Block = [] for stage in stages {
 for show in stage.shows {
 let indices = blocks.filterAndMapIndex { block in block.hasOverlap(show) }
 if indices.isEmpty { 
 blocks.append(newBlock(show))
 } else {
 var mergedBlock = blocks[indices[0]]
 mergedBlock.add(show)
 for i in 1..<indices.count {
 mergedBlock.merge(blocks.remove(at: indices[i]))
 }
 }
 }
  6. ϚΠςʔϒϧͷηϧͷ݁߹ϧʔϧͷܭࢉ • શମͷྻ෯ͱBlock಺ͷྻ෯ͷؔ܎ • શମͷྻ෯ΛWɺશମͷεςʔδ਺N • Block಺ͷεςʔδ਺n͸1..NͷͲΕ͔ • Block಺ͷηϧͷԣ෯w͸1/n *

    W • ηϧͷ݁߹ϧʔϧ͸੔਺ͱͯ͠දݱ͢Δඞ ཁ͕͋Δ • ͭ·Γશͯͷ1/n * W͕શͯ੔਺ʹͳΔ Α͏ʹWΛઃఆ͢Δඞཁ͕͋Δ
  7. ϚΠςʔϒϧͷηϧͷ݁߹ϧʔϧͷܭࢉ • WΛ1..Nͷ࠷খެഒ਺͢Δͱྑ͍ • N=4ͷͱ͖ɺW=12 • 4εςʔδ … w= 3

    • 3εςʔδ … w = 4 • 2εςʔδ … w = 6 • 1εςʔδ … w = 12 O O O O
  8. ෳ਺ͷ਺ͷ࠷খެഒ਺ͷ࣮૷ • ෳ਺ͷ࠷খެഒ਺ˡ̎਺ͷ࠷খެഒ਺
 func lcm(v: [Int]) { return v.reduce(1) {

    lcm($0, $1) } } • ̎਺ͷ࠷খެഒ਺ˡ2਺ͷ࠷େެ໿਺
 func lcm(m: Int, n: Int) { return m * n / gcd(m, n) } • ࠷େެ໿਺ͱ࠷খެഒ਺ͷੵ͕΋ͱͷೋͭͷ਺ͷੵʹ౳͍͠ • 2਺ͷ࠷େެ໿਺ˡϢʔΫϦουͷޓআ๏
 func gcm(m: Int, n: Int) {
 if n == 0 {
 return m
 }
 if m < n {
 return gcd(n, m)
 }
 return gcd(n, m % n)
 }
  9. ·ͱΊ • UIScrollViewΛ̏ͭ૊Έ߹ΘͤΕ͹ɺλΠϜςʔϒϧ͸ ࣮૷Ͱ͖Δ • ζʔϜ΍ճసରԠ͸݁ߏେม • ͜ͷลΛϥΠϒϥϦʹऔΓࠐΉ͸Too much͔΋ •

    ηϧͷදࣔͷͤ͞ํΛ͍͍ײ͡ʹ͍ͨ͠ͱࢥ͍ͬͯͨΒɺ ͍ͭͷؒʹ͔ϢʔΫϦουͷޓআ๏Λ࣮૷͍ͯͨ͠