Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Farewell SnapKit
Search
kameike
July 26, 2018
Programming
1.9k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Farewell SnapKit
ありがとう!!!!さようなら!(?)
kameike
July 26, 2018
More Decks by kameike
See All by kameike
LeanScrum
kameike
3
1.3k
橋の下で安心して寝るための 『iOSアプリ設計パターン入門』
kameike
0
2.2k
サポートを求めてAppleに電話するまでの道のり
kameike
4
2.4k
header-comment
kameike
5
1.6k
Other Decks in Programming
See All in Programming
なぜCTOを降りてFDEを選んだのか?〜なぜプロダクト企業がFDEで顧客の現場に踏み込むのか〜
gonta
1
240
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
420
学びを記事・スライド・Xへ高速展開する― AI-assisted Learning Loopで回すコンテンツ生成術
ochtum
0
150
GraphQL の onError と Nullability のこれから
quramy
0
180
FactoryBotをやめればRSpecは速くなるのか?
yuta_onishi_97
0
1.2k
golang-bob.pdf
iwatsukayura
0
140
コードで差別化できない時代の ソフトウェアエンジニアリング / Software Engineering in an Era Where Code Doesn’t Differentiate
kishida
2
200
作れる、でも消せるか?──安全に「枯らして消す」機能削除術 / Drain, Then Delete
chobishiba
2
1k
Python Lambdaにおけるテストしやすい設計とAI支援の活用 / Designing Testable Python Lambda Functions with AI
kitano_yuichi
1
200
AmebaブログはKMPから卒業します
kmars0605
0
240
現場と育てるRailsモデリング 〜Spreadsheetから始めたモデル設計〜
siroemk
2
960
Railsバージョンを4倍にした話
optim
0
730
Featured
See All Featured
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
470
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
580
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
1
640
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
340
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
550
How to Ace a Technical Interview
jacobian
280
24k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
300
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
ラッコキーワード サービス紹介資料
rakko
4
5.5M
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Transcript
Farewell SnapKit さようなら、ありがとうSnapkit! pixiv Inc. kameike 亀田 彗 2018.6.5
- パルシィというアプリを書いているもの です - 10月に福岡から上京 2 だれ @kameike
宣伝
• SnapKitよりもAnchorがDXが良いと思うよという話 • 最近わりと pod SnapKitを見かけたので... • 13,000スターのスーパースター。ありがとうの気持ち、大切! 4 今日の話の要約
5 前提
6 AutoLayout(viewに制約を追加するやつ) をコードで作りたい!
let constraints = NSLayoutConstraint( item: subview, attribute: .left, relatedBy: .equal,
toItem: view, attribute: .left, multiplier: 1.0, constant: 0.0) 普通に作る 10
let constraints = NSLayoutConstraint.constraints( withVisualFormat: "H:|-15-[view]-15-|", metrics: nil, views: ["view":
subView]) 例のアレ(VisualFormatLanguage) 10
10
10
// SnapKit subview.snp.makeConstraints { constraintMaker in constraintMaker.top.equalTo(view) constraintMaker.bottom.equalTo(view) constraintMaker.left.equalTo(view) constraintMaker.right.equalTo(view)
} SnapKit 10
// Anchor NSLayoutConstraint.activate([ subview.topAnchor.constraint(equalTo: view.topAnchor), subview.bottomAnchor.constraint(equalTo: view.bottomAnchor), subview.trailingAnchor.constraint(equalTo: view.trailingAnchor), subview.leadingAnchor.constraint(equalTo:
view.leadingAnchor) ]) Anchor(iOS9~) 10
13 書き方こそ似ているけども、 Anchorの方がDXが良いぞ〜! (あとUIKitだし...寿命も長いはず...)
14 Anchorのよさ
// SnapKit subview.snp.makeConstraints { constraintMaker in constraintMaker.top.equalTo(_ other: ConstraintRelatableTarget) }
// ConstraintRelatableTargetとして使える型 // Int UInt Float Double CGFloat CGSize CGPoint // UIEdgeInsets, UIView SnapKit 10
16 暗黙的...
SnapKit 10
// Anchor subview.topAnchor.constraint(_ equalTo: NSLayoutAnchor<NSLayoutYAxisAnchor>) Anchor 10
Anchor 10
20 よさ2 ありえない組み合わせが 型によって排除されている
// SnapKit subview.top.equalTo(superView.snp.right) このようなコード 10
期待される挙動 10
23 実行時エラー:sob:
// Anchor view.topAnchor.constraint(equalTo: superView.leadingAnchor) Anchorだと...! 10
// Anchor view.topAnchor.constraint(equalTo: superView.leadingAnchor) このようなコード 10 NSLayoutXAxisAnchor
26 コンパイルエラーになる:joy:
// SnapKit subview.top.bottom.equalTo(view).offset(10).offset(20) ちなみにSnapKitはこんなコードを コンパイルできてしまう 10
28 Anchor便利...!
29 SnapKit->Anchorで 移行Tips
30 基本的にはすんなり行くはず!
// SnapKit let offset = UIEdgeInsets(top: 10, left: 10, bottom:
10, right: 10) make.edges.equalTo(view).offset(offset) Edgeがほしい! 10
extension UIView { func edges(equalTo view: UIView, offset: UIEdgeInsets =
UIEdgeInsets()) { NSLayoutConstraint.activate([ topAnchor.constraint(equalTo: view.topAnchor, constant: offset.top), bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -offset.bottom), leftAnchor.constraint(equalTo: view.leftAnchor, constant: offset.left), rightAnchor.constraint(equalTo: view.rightAnchor, constant: -offset.right) ]) } } extensionでなんとかなる!10行ぐらい! 10
// SnapKit view.edges.equalTo(view).offset(UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
// Anchor+Extension view.edges(equalTo: view, offset: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)) なんとかなった...! 10
// leading, trailing extension UIView { func edges(equalTo view: UIView,
offset: NSDirectionalEdgeInset) { NSLayoutConstraint.activate([ topAnchor.constraint(equalTo: view.topAnchor, constant: offset.top), bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -offset.bottom), leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: offset.leadingAnchor), trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -offset.trailingAnchor) ]) } } 余談 leading trailingを使いたいなら 10
let offset = 3 * 5 // SnapKit だと大丈夫だけど make.top.equalTo(view).offset(offset)
// Anchor だとコンパイルエラー subview.topAnchor.constraint(equalTo: view.topAnchor, constant: offset) ConstraintXXXTarget的なものを CGFloatにする必要がある 10
36 余談の余談
@available(iOS 6.0, *) open func addConstraint(_ constraint: NSLayoutConstraint) // This
method will be deprecated in a future release and should be avoided. // Instead, set NSLayoutConstraint's active property to YES. 10 addConstraintは 使わない方がいい
let constraint = view.bottomAnchor.constraint(equalTo: bottomAnchor) constraint.isActive = true // もしくは
NSLayoutConstraint.activate([constraint]) // でconstraintを有効に...! こういう感じで 10
39 SnapKit余談
• 7/17にSwift4対応!(そろそろ保守されなくなってきてるって言おうとおもっていたのに) • ので、まだ慌てる時間じゃない...!? 40 まだしっかり保守されている...!
• 3000行の本体コード(RxCocoaの1/3ぐらいコードがある...!) • 大体全ての型にMapperを書いてDSL上では独自型だけで表現できる世界を実現してい る(その分よしなであり、上述の問題もあり ...) 41 整ったコード(整いすぎでは...?!)
42 Priorityに若干のカスタマイズ public static var medium: ConstraintPriority { #if os(OSX)
return 501.0 #else return 500.0 #endif }
• 時々example作ったからマージしてよ!という PRがきていた • 苦労をかんじる最適化 43 13000スターつくと何が起こるか
• SnapKit→Anchor • ありがとうSnapKit!さようなら!(?) 44 まとめ