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

AutoLayout Algorithm

AutoLayout Algorithm

Yasuhiro Inami

September 16, 2017
Tweet

More Decks by Yasuhiro Inami

Other Decks in Programming

Transcript

  1. Auto Layout + Code view.addConstraints([ NSLayoutConstraint( item: view1, attribute: .centerX,

    relatedBy: .equal, toItem: view2, attribute: .centerX, multiplier: 1.0, constant: 0 ), ... ])
  2. Auto Layout + Visual Format let views = ["v1": view1,

    "v2": view2] let metrics = ["d": 40, "w1": 100, "w2": 200] let options: NSLayoutFormatOptions = [] let constraints = NSLayoutConstraint.constraints( withVisualFormat: "H:|-(>=d,<=100@250)-[v1(>=w1@750)]-[v2(>=w2@1)]-|", options: options, metrics: metrics, views: views ) view.addConstraints(constraints)
  3. Auto Layoutͷಛ௃ • ʮଐੑ1 = ܎਺ × ଐੑ2 + ఆ਺ʯͷؔ

    ܎ੑʢ੍໿ʣΛ એݴతʹهड़Ͱ͖Δ • Ϗϡʔ֊૚ʹґଘ͠ͳ͍ • ෆ౳ࣜɺ༏ઌ౓ɺݻ༗αΠζɺऩॖ ੑɾ๲ுੑ • Xcode ʹΑΔGUIαϙʔτ • ಠࣗDSL (Visual Format) • UIStackView΋Auto Layoutϕʔε
  4. or

  5. import Accelerate // LU෼ղ͔Β࿈ཱํఔࣜΛղ͘ func solve(_ A: [Double], _ B:

    [Double]) -> [Double] { var inMatrix = A, solution = B var N = __CLPK_integer( sqrt( Double( A.count ) ) ) var N2 = N, N3 = N, LDA = N, LDB = N var NRHS: __CLPK_integer = 1 var pivots = [__CLPK_integer](repeating: 0, count: Int(N)) var error: __CLPK_integer = 0 dgetrf_(&N, &N2, &inMatrix, &N3, &pivots, &error) var t = "T".cString(using: .utf8)!.first! dgetrs_(&t, &N, &NRHS, &inMatrix, &LDA, &pivots, &solution, &LDB, &error) return solution }
  6. ྫɿੜ࢈ܭը໰୊ 4छྨͷݪྉAɺBɺCɺDΛ༻͍ͯɺ3छྨͷ੡඼XɺYɺZΛੜ ࢈͢Δ޻৔͕ɺ࠷େͷརӹΛ͋͛ΔͨΊʹੜ࢈ܭըΛཱͯΔɻ • ݪྉͷ૯ྔɿA = 80kgɺB = 50kgɺC =

    100kgɺD = 70kg • ੡඼ͷՁ֨ɿX = 70ສԁɺY = 120ສԁɺZ = 30ສԁ • ੡඼ίετɿX͸1୯Ґ͋ͨΓ (A, B, C, D) = (5, 0, 7, 3) ͷίε τɺY͸ (0, 2, 0, 11)ɺZ͸ (6, 8, 15, 0) • ૯རӹ = Λ࠷େԽ͍ͨ͠
  7. γϯϓϨοΫε๏ (୯ମ๏) • ઢܗܭը໰୊͕ղ͚ͳ͍έʔε • ࣮ߦෆՄೳʢ࣮ߦՄೳղ͕ଘࡏ͠ͳ͍ʣ • ඇ༗քʢ໨తؔ਺Λ͍͘ΒͰ΋վળͰ͖ͯ͠·͏ʣ • ࠷খఴ͑ࣈنଇ

    (Bland rule) ... ແݶϧʔϓରࡦ • ໨తؔ਺ͷ܎਺Λॱʹฒ΂ͯɺ࠷ॳʹෛʹͳΔʢ࠷খఴ͑ ࣈʣม਺Λબͼɺ ͷީิʹ͍ͭͯ΋࠷খఴ͑ࣈΛબͿ
  8. ૒ରγϯϓϨοΫε๏Λ࢖͏λΠϛϯά • ઢܗܭը໰୊ͷ࠷ద஋ͱ࠷దղ͕͢Ͱʹٻ·͓ͬͯΓɺ౳ࣜ ੍໿ ͷ Λ ʹஔ͖׵͍͑ͨ৔߹ɺҰ͔ΒγϯϓϨ οΫεදΛ࠶ߏஙʢओ໰୊Λղ͖௚͢ʣ͠ͳͯ͘ྑ͍ • ͳͥͳΒɺ૒ର໰୊ͷ੍໿͸ɺ

    ʹରͯ͠ෆมͰɺ૒ର ໰୊ͷ࣮ߦՄೳੑ͸อͨΕΔʢͨͩ͠ඇෛ৚݅͸յΕΔʣͷ Ͱɺओ໰୊Λ૒ର໰୊ʹม׵ͯ͠ɺγϯϓϨοΫε๏Λద༻ ͢Ε͹ྑ͍ͨΊ
  9. ྫ2ɿࠨਤͷ (1) ͷ੍໿ͷू߹͔Βɺ (2) ͷγϯϓϨοΫεදΛಘͨޙɺ ʢ ɺϚʔΧʔ͸ ʣ੍໿Λ ࡟আ͢Δ৔߹Λߟ͑Δɻ ·ͣɺ

    ͱͳΔߦ͕ͳ͍ͨΊɺ୯ ७ͳߦ࡟আ͸Ͱ͖ͳ͍ɻ ͦ͜Ͱɺ࠷খൺϧʔϧΛ༻͍ͯɺ ͷߦΛબͼɺ(3) ͷܗʹมܗʴଞͷߦʹ୅ ೖͨ͠ޙɺ(4) બΜͩߦΛ࡟আ͢Δɻ ʢ࡟আલ͸Ұ࣌తʹɺ࣮ߦՄೳղʹͳΒ ͳ͍͕ɺ࡟আޙ͸࣮ߦՄೳղͱͳΔʣ
  10. ·ͱΊ (1) • CassowaryΛཧղ͢Δͷ͸ɺҰݟɺ೉͘͠ݟ͑Δ! • Ͱ΋ɺࠓ·Ͱ࢖ͬͨܭࢉ͸ʮ଍͠ࢉʯʮֻ͚ࢉʯʮ؆୯ͳߦ ྻૢ࡞ʯͷΈ → ඍੵ෼ͳͲ͸ग़ͯ͜ͳ͍ •

    ߴߍ਺ֶͷ஌͕ࣝ͋Ε͹ɺे෼ཧղͰ͖Δ • Ή͠Ζɺӳޠத৺ͷ࿦จ΍৘ใΛಡΉํ͕େม • ӳޠ >>>>> ਺ֶ
  11. ·ͱΊ (2) • Cassowary͸ม਺Λʮ޷͖ͳ͚ͩʯ੍໿ʹ࢖͏͜ͱ͕Մೳ • Auto Layout͸ɺʮ2ͭͷଐੑʯͷؔ܎ੑͷΈ Cassowary͕ڧྗͳྫ // superView,

    view1, view2ߏ੒Ͱɺ౳ִؒʹ഑ஔ (4ଐੑؒͷؔ܎ੑ) view1.left - superView.left = view2.left - view1.right view1.left - superView.left = superView.right - view2.right
  12. References • Alan Borning, Kim Marriott, Peter Stuckey, and Yi

    Xiao. Solving Linear Arithmetic Constraints for User Interface Applications (1997) • ࡉ෦ ത࢙. ϢʔβΠϯλϑΣʔεͷͨΊͷઢܗ౳ࣜɾෆ౳ࣜ ੍໿ղফܥ (2002) • pybee/cassowary (Python version) • robb/Cartography (Auto Layout wrapper in Swift)