今のうちに知っておきたい swiftの高速化 + 3d touch api
TRANSCRIPT
• Home Screen Quick Actions • UIKit peek and pop API • Web view peek and pop API • UITouch force properties
※ Apple Developer Libraryで一般に公開されている情報に基きます
3D Touch APIs
Static quick actionsUIApplicationShortcutItemsのarrayをinfo.plistに記述する
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: Bool -> Void)
が呼ばれるので、ごにょごにょする
起動時の場合はlaunchOptionsに UIApplicationLaunchOptionsShortcutItemKey があるので、そっちを使う。notificationやopenURLの処理とかと同じ感じ
Dynamic quick actionsUIApplication.shortcutItems に UIApplicationShortcutItemを登録
ログインやユーザ登録等が必要な場合はこちらを使うっぽい
4つまで登録が可能
sample code: ApplicationShortcuts: Using UIApplicationShortcutItem
• 受信ボックスのEメールをPeek -> Popで普通に開く • リンクをPeek -> PopでSafari • カメラ画面でサムネイルをPeek -> Popで全画面 • 住所をPeek -> Popでマップ • 顔アイコンを3D Touchで (Facetime、メッセージ、メール等) • 左スワイプでメール削除、右スワイプで未読に?
使用例
• Appleの日本語ページによれば、Peekが覗く Pop で開くというニュアンス
• UIViewControllerPreviewingDelegate
• UIPreviewAction
• UIPreviewActionGroup
• UIPreviewActionItem (protocol)
前準備3D Touchの対応チェック
traitCollection.forceTouchCapability == .Available
3D Touchを検出するViewを登録
registerForPreviewingWithDelegate(self, sourceView: view)
解除は
unregisterForPreviewingWithContext(previewing: UIViewControllerPreviewing)
ViewControllerにUIViewControllerPreviewingDelegateを実装
Peekさせる ViewController を返す感じ
func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController?
Popさせる 時に呼ばれるのでshowViewControllerする感じ
func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController)
UIViewControllerPreviewingDelegate
Peekを上へスワイプした時にActionSheetっぽいやつが表示される
override func previewActionItems() -> [UIPreviewActionItem]
で、UIPreviewActionItemのリストを返す
UIPreviewActionGroup を使って複数のItemを内包させることもできる。 どうなるかは未確認。。
UIPreviewActionItem
WKWebView.allowsLinkPreviewをtrueにすると有効になる
Pop時にはSafariじゃなくてSFSafariViewControllerが開くみたい。 JavascriptのAPIが出来れば良いのになと。
使い方
• UITouch に force, maximumPossibleForce のプロパティが増えた • 9.1はPencil関係、ペンの角度も取れるみたいでヤバイ
UITouch.force, UITouch.maximumPossibleForce
Swift 2 採用して数ヶ月• だんだんコーディング規約のようなものも出来てきた
• selfを使わない
• セミコロンなしで統一
• 可能な限り型定義しない
• クロージャは省略した書き方を優先する
• Voidは省略等
前提これからお見せするのは 2015/09/10 iOS 9 GM Seed 発表時点、iPhone 6上で実行結果です
一部を除き1,000,000回ループさせた時にかかった時間です
TestSpleehttps://github.com/jstarfruits/TestSplee
-------------------- 0.008 0.007 0.008 0.008 0.008 -------------------- Avg: 0.008 ====================
VS
0.021 0.020 0.020 0.021 0.021 -------------------- Avg: 0.021
0.377 0.374 0.373 0.372 0.374 -------------------- Avg: 0.374
VS
0.234 0.231 0.233 0.233 0.234 -------------------- Avg: 0.233
0.568 0.572 0.572 0.573 0.569 -------------------- Avg: 0.571
0.836 0.832 0.835 0.830 0.831 -------------------- Avg: 0.833
0.089 0.090 0.090 0.089 0.089 -------------------- Avg: 0.090
0.343 0.343 0.342 0.343 0.342 -------------------- Avg: 0.343
VS
0.446 0.446 0.450 0.448 0.443 -------------------- Avg: 0.447
0.211 0.210 0.212 0.211 0.212 -------------------- Avg: 0.211
0.630 0.624 0.625 0.624 0.625 -------------------- Avg: 0.626
0.229 0.227 0.229 0.227 0.228 -------------------- Avg: 0.228
0.227 0.227 0.228 0.226 0.227 -------------------- Avg: 0.227
0.059 0.060 0.059 0.060 0.059 -------------------- Avg: 0.059
0.167 0.168 0.169 0.167 0.169 -------------------- Avg: 0.168
6.690 6.751 6.810 6.784 6.755 -------------------- Avg: 6.758
0.542 0.537 0.537 0.539 0.538 -------------------- Avg: 0.538
0.043 0.044 0.044 0.042 0.042 -------------------- Avg: 0.043
0.746 0.744 0.755 0.747 0.745 -------------------- Avg: 0.747
0.286 0.280 0.294 0.288 0.285 -------------------- Avg: 0.286
0.009 0.009 0.009 0.009 0.009 -------------------- Avg: 0.009
2.941 2.662 2.635 2.646 2.635 -------------------- Avg: 2.704
0.199 0.199 0.197 0.210 0.200 -------------------- Avg: 0.201 引数が1つなら0.026まで短縮
0.092 0.092 0.092 0.094 0.092 -------------------- Avg: 0.092
0.107 0.106 0.106 0.107 0.107 -------------------- Avg: 0.107
まとめ• 3D Touch はやくさわりたい
• 書き方次第で10倍以上スピードが上がる
• 規約に悩んだらスピードで決めるのって良い
• 遅いのはObjective-Cのせいじゃない
• なぜ速いのか速いのか? 中間コードを読めれば・・・