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
Transcript
44 slides
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 まとめ
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
止まらない比較を止める — 余帰納法で無限ストリームを比べる
tarao
0
190
Claude Codeのおかげでやりたいことがやれるようになったんだよォー!
sms_tech
0
160
AI時代のFlutter品質設計 — アプリ品質を守るQuality Layers
omatty198
0
390
Ruby から Emacs を操作したい
phigasui
0
360
Web3Dライブラリを作ってVue Custom Rendererの仕組みを理解しよう / lets learn vue custom renderer by making 3d library
drumath2237
1
140
flutter_test のその先へ — Flutter アプリの LLM 機能に「評価」という品質保証を
kosukesaigusa
0
200
なぜ私たちは「現時点では」KMPの導入を見送ったのか
chigichan24
1
310
Spec-driven Development with AI
simas
PRO
0
2.3k
コード生成時代のAPI定義を支えるRequest Spec移行
pvcresin
0
290
Vue.jsで作る空間解析アプリとCapacitorプラグインのData Bridge
hal_spidernight
1
180
LLM 101
dalinaum
0
510
地方こそサーバーレス — 10年続けた、顔が見えるチームで確かな基盤を / Serverless Matters Most in Regional Areas — 10 Years of Building Reliable Foundations with Close-Knit Teams
seike460
PRO
2
490
Featured
See All Featured
A Soul's Torment
seathinner
9
3.9k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Un-Boring Meetings
codingconduct
0
500
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
880
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
3
3.9k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.4k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.9k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
580
The Invisible Side of Design
smashingmag
301
52k
Paper Plane
katiecoart
PRO
6
55k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
750
Evolving SEO for Evolving Search Engines
ryanjones
0
360