WWDC15 にて watchOS 2 が発表され、ついに待望のネイティブ動作する Apple Watch アプリがつくれるようになりました。それに伴い、WatchKit には多くの機能が追加され、ClockKit や WatchConnectivity 等の新しいフレームワークも登場しました。さらにいえば、従来フレームワークもウォッチ側で動作するようになったことから、これらもある意味新APIであるといえます。
どんな機能が追加されたのかは既に多くのキーノートのまとめ記事などで紹介されてはいますが、新しいAPIはどう使うのか、実際に何がどこまでできるのか、快適に動作するのか、といった具体的なところが、英語のドキュメントや動画や記事をながめているだけだと正直あまりよくわかりません。やはり実際にコード書いて動かしてみるのが一番わかりやすい、ということで、
watchOS 2 の新機能のサンプルコード集『watchOS-2-Sampler』
をつくりました。オープンソースなのでどなたでも GitHub から clone してお試しいただけます。
##Contents
今のところ18個のサンプルが入っています。まだまだ少ないですが、随時追加していく予定です。機能追加等のプルリク大歓迎です!
![](https://qiita-user-contents.imgix.net/http%3A%2F%2Ff.st-hatena.com%2Fimages%2Ffotolife%2Fs%2Fshu223%2F20150614%2F20150614131615.jpg?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=dca7fe9a862195d7d4512db000538e92)
###Animated Properties
animateWithDuration:animations:
メソッドを利用して、アニメーションで拡大縮小やフェードイン/アウト、移動(実際には Alignment)を行うサンプル。
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FwatchOS-2-Sampler%2Fmaster%2FResourcesForREADME%2Fanimation.gif?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=bf64923ee740337abc7f99ada40ca523)
###Table Animations
WKInterfaceTable のセル(row)をアニメーション付きで insert / remove するサンプル。insertRowsAtIndexes:withRowType:
, removeRowsAtIndexes:
メソッドを利用。
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FwatchOS-2-Sampler%2Fmaster%2FResourcesForREADME%2Ftableanim.gif?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=a4add4bba92a733016f14312fa4f49d6)
###Picker Styles
WKInterfacePicker の全スタイル(List, Sequence, Stack)を試せるサンプル。
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FwatchOS-2-Sampler%2Fmaster%2FResourcesForREADME%2Fpicker.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=7a0e589bb4023050bd7ba8de2ce54659)
###Taptic Engine
WKInterfaceDevice の playHaptic
メソッドに指定できる全タイプ(WKHapticType)を試せるサンプル。
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FwatchOS-2-Sampler%2Fmaster%2FResourcesForREADME%2Ftaptic.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=0430b2f830e1f018236172816fc3275e)
ちなみに WKHapticType は次のように定義されています。
enum WKHapticType : Int {
case Notification
case DirectionUp
case DirectionDown
case Success
case Failure
case Retry
case Start
case Stop
case Click
}
###Audio Rec & Play
presentAudioRecordingControllerWithOutputURL:preset:maximumDuration:actionTitle:completion:
メソッドを利用して音声を録音する UI を表示し、録音したファイルを presentMediaPlayerControllerWithURL:options:completion:
メソッドを利用して再生するサンプル。
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FwatchOS-2-Sampler%2Fmaster%2FResourcesForREADME%2Frec_play.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=31fa9098af8e13d10bee8506e8665d0e)
参考記事:
###Animation with Digital Crown
デジタルクラウンの回転に合わせ、WKInterfacePicker にセットしたアニメーションを表示するサンプル。setCoordinatedAnimations
メソッドを利用。
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FwatchOS-2-Sampler%2Fmaster%2FResourcesForREADME%2Fcrownanim.gif?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=11b643f5429a824bea44cdd102b40022)
###Draw Paths
Core Graphics を用いてパスを描画するサンプル。
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FwatchOS-2-Sampler%2Fmaster%2FResourcesForREADME%2Fbezier.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=8b9e72034a76710c843715b23c1f9199)
###Gradations
Core Graphics を用いてグラデーションを描画するサンプル。
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FwatchOS-2-Sampler%2Fmaster%2FResourcesForREADME%2Fgradation.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=23e052fb22b46afe0ac11f5c872cde8c)
※Core Graphics を用いたパス描画やグラデーション描画についての詳細は、こちらの記事・スライドもご参照ください。
- http://d.hatena.ne.jp/shu223/20150714/1436875676
- http://www.slideshare.net/t26v0748/core-graphics-on-watchos-2
###Heart Rate
HealthKit を用いて心拍数を取得・表示するサンプル。
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FwatchOS-2-Sampler%2Fmaster%2FResourcesForREADME%2Fheartrate.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=ef25ea935e6c02751054e5f00a8f5fcd)
kitasuke さんより pull request いただきました。
###Accelerometer
Core Motion を用いて加速度センサの値を取得するサンプル。
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FwatchOS-2-Sampler%2Fmaster%2FResourcesForREADME%2Facc.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=00ee90e0ff16cebc61d733078f16ce33)
###Gyroscope
Core Motion を用いてジャイロスコープの値を取得するサンプル。 1
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FwatchOS-2-Sampler%2Fmaster%2FResourcesForREADME%2Fgyro.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=cce3c79e975707d44700a458d95a8725)
###Device Motion
Core Motion を用いてデバイスのモーションデータを取得するサンプル。 2
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FwatchOS-2-Sampler%2Fmaster%2FResourcesForREADME%2Fdevmotion.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=e8cfc1476282c95194800574aee3ab22)
###Pedometer
CMPedometer を用いて歩数や距離、階の昇り降り情報を取得するサンプル。
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FwatchOS-2-Sampler%2Fmaster%2FResourcesForREADME%2Fpedometer.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=928b0bb58b286094b79b585cde23a070)
###Alert
presentAlertControllerWithTitle:message:preferredStyle:actions:
メソッドで指定できる全アラートスタイル(WKAlertControllerStyle)を試せるサンプル。
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FwatchOS-2-Sampler%2Fmaster%2FResourcesForREADME%2Falert.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=5a11e83e3adf951fa12a02aac2a8d1cc)
ちなみに WKAlertControllerStyle は次のように定義されています。
enum WKAlertControllerStyle : Int {
case Alert
case SideBySideButtonsAlert
case ActionSheet
}
###Interactive Messaging
iPhone と Apple Watch で相互にメッセージをやりとりするサンプル。Watch Connectivity フレームワークを利用。
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FwatchOS-2-Sampler%2Fmaster%2FResourcesForREADME%2Finteractive.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=6707c0b8b0ecf4b967ba7553ba4d30c0)
###Audio File Player
WKAudioFilePlayer を利用してオーディオファイルを再生するサンプル。
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FwatchOS-2-Sampler%2Fmaster%2FResourcesForREADME%2Faudiofile.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=a7e4e7c63f2791e306efa2f09c3ceae8)
※要Bluetoothヘッドセット
###Open System URL
WKExtension の openSystemURL:
メソッドを利用して、電話およびSMSアプリを開くサンプル。
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FwatchOS-2-Sampler%2Fmaster%2FResourcesForREADME%2Fopen.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=d40c355d4cfcfcb7f7ab18a64d0569a1)
###Network Access
NSURLSession を用いてネットワーク経由で画像データを取得するサンプル。KAMEDAkyosuke さんより pull request いただきました。
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FwatchOS-2-Sampler%2Fmaster%2FResourcesForREADME%2Fnetwork.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=f7b8506e4cf771be480ee18956a398aa)
##おまけ:watchOS 2 で使えるようになった従来フレームワークのリスト
- MapKit
- UIKit
- WatchKit
- CoreGraphics
- ImageIO
- CFNetwork
- Contacts
- CoreData
- CoreFoundation
- CoreLocation
- CoreMotion
- EventKit
- Foundation
- HealthKit
- HomeKit
- MobileCoreServices
- PassKit
- SystemConfiguration
- Core OS Layer
- Security
##iOS-9-Sampler
iOS 9 の新機能のサンプル集はこちらにあります。
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FiOS-9-Sampler%2Fmaster%2FResourcesForREADME%2Fmap.gif?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=535861027d127ddd88ff102403803fe3)
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FiOS-9-Sampler%2Fmaster%2FResourcesForREADME%2Fimagefilters.gif?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=3761091c28c9a9aebf7eaaaf136fff25)
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fraw.githubusercontent.com%2Fshu223%2FiOS-9-Sampler%2Fmaster%2FResourcesForREADME%2Fuistackview.gif?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=05949d89ff2f63fbe4a1d64ce49bceea)
##関連
- watchOS 2 で本当にウォッチ側で処理されているのか確認する
- iOS 8 の新機能のサンプルコード集『iOS8-Sampler』
- iOS 7 の新機能のサンプルコード集『iOS7 Sampler』