Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Farewell SnapKit
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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.5k
Other Decks in Programming
See All in Programming
ソフトウェアエンジニアにとっての生成AI - 特性を知って使い倒す / generative ai for software enginner
kishida
7
2.1k
そこに3びきプロダクトがいるじゃろう——生成AI時代における“価値が届かない理由”の構造
kosuket
0
570
AWS DevOps AgentのAzure接続機能を検証して見えた活用法/Use Cases Verified for the AWS DevOps Agent's Azure Connectivity Feature
masakiokuda
1
270
リアルな遅延を測る仕様
kota_yata
1
120
PyConJP2026_wat_Python × Signal Processing: How to Draw Pictures with Sound Using Spectrogram Art
wat
0
340
PostgreSQL 18で考えるUUID主キー
kazuhiro1982
0
500
レビュー履歴をAIに食わせて、 Compose移行を加速するs
shihochan
0
200
属人化した知識を、 AIが辿れる地図にする
pkshadeck
PRO
1
200
S3 を使うアプリケーションをローカル完結で動かすことに全力を注いでみた / Running S3 Apps Offline
contour_gara
0
600
AI Readyの正体はデータマネジメントだ メダリオン2.0の最前線
freee
PRO
0
370
わからない話を追いかけたら、プログラミング言語を作る側にいた
ydah
3
540
生成AIで帳票OCRが「簡単に」作れる時代になった?
kon_shou
0
1.2k
Featured
See All Featured
Design in an AI World
tapps
1
290
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
440
Building AI with AI
inesmontani
PRO
1
1.2k
The Pragmatic Product Professional
lauravandoore
37
7.4k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
Leo the Paperboy
mayatellez
8
2.2k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.4k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.4k
ラッコキーワード サービス紹介資料
rakko
1
4.5M
GitHub's CSS Performance
jonrohan
1033
470k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.2k
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 まとめ