LoginSignup
4
5

More than 5 years have passed since last update.

開発している時困ったこと+資料が少ないもののWatchKitTips

Last updated at Posted at 2016-01-15

開発している時困ったこと+資料が少ないものをピックアップして書いていきます。

アニメーションなしの画面遷移を実装する

applewatchでの画面遷移はmodalとpresentの2つしかありません。なので、アニメーションなしの画面遷移ができません。
アニメーションなし画面遷移を行いたい場合は同一シーンに2つのシーンのアイテムを入れ込み表示・非表示させる方法を取る必要があります。私はそれぞれのシーンごとにグループを作り、そのグループにそのシーンに必要なものを入れ込みました。ただ、この方法であまり多くのシーンを詰め込むとプログラムが長くなるので注意が必要です。

移動前のインターフェイスの関数を実行する方法

いわゆるシーンやビューと言われるものはAppleWatchではインターフェイスと呼びます。
別のインターフェイスに移動する時は

self.presentControllerWithName("ModalInterface", context: self)

で移動します。contextにはselfを入れて遷移先のインターフェイスに渡します。
受け取った方は

class ModalInterfaceController: WKInterfaceController
{
    var delegate: InterfaceController?

    override func awakeWithContext(context: AnyObject?)
    {
        super.awakeWithContext(context)
        self.delegate = context as? InterfaceController
        self.delegate?.tergetfunction()
    }

    @IBAction func test1Button()
    {  
        self.delegate?.tergetfunction()
        self.dismissController()
    }   
}

にします。これで、前のインターフェイスの関数が実行できます。

インターフェイスのIDを調べる

どこかインターフェイスでエラーが起こっていてもインターフェイスのIDで書かれていて一体どのインターフェイスでエラーが起こっているか特定できません。
そういった時、以下のコードをどかに挿入すればインターフェイスのIDを確認できます。

if let vcID = self.valueForKey("_viewControllerID") as? NSString
{
        print("Page ID : \(vcID)")
}

また、インターフェイスと紐付けされたクラス名がわかると便利なので

if let vcID = self.valueForKey("_viewControllerID") as? NSString
{
        print("Class : \(NSStringFromClass(self.dynamicType))\nPage ID : \(vcID)")
}

とすれば調査しやすくなる。

使用しているwatchが38mmか42mmか確認する

AppleWatchは画面サイズが38mmと42mmの2種類あるので画面によって文字サイズ等を変えたい時は不便です。画面サイズは確認できるのでそのサイズによってwatchのサイズを判定します。

let currentDevice = WKInterfaceDevice.currentDevice()
let bounds = currentDevice.screenBounds
// 38mm: (0.0, 0.0, 136.0, 170.0)
// 42mm: (0.0, 0.0, 156.0, 195.0)

if bounds.width > 136.0 {
     print("This is the big watch")
} else {
     print("This is the small watch")
}
4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5