LoginSignup
6
2

More than 5 years have passed since last update.

以前、tvOSアプリケーション開発を試したことがあるが、あれから時間が経過したのと、tvOSのAdvent Calendarに投稿する機会に恵まれたということで、再挑戦することにした。幸い、他の投稿記事には入門的なものはないようなので、ちょうどいいと思っている。

tvOSのアプリケーションには二種類ある。従来型とクライアント-サーバ型だ。

後者はメディアのストリーミングを行うアプリケーションで従来のWeb技術を活用するものだ。マークアップ言語のTVMLでインターフェイスを実装し、JavaScriptで挙動を記述する。TVMLKitフレームワークはネイティブコードとの橋渡しをするものだ。

この投稿では、前者の従来型を取り上げる。Swiftで実装するアプリケーションだ。

サンプル・アプリケーションを用意したので、これを使って説明する。独自のコンテナViewControllerを使って画面を切り替えている。

コンテナビューコントローラ.png

ContainerViewControllerは子となるViewControllerをインスタンス変数で保持する。

class ContainerViewController: UIViewController {
    var titleViewController: TitleViewController?
    var gameViewController: GameViewController?

Storyboardから子となるViewControllerを取得し、子ViewControllerに親となるコンテナViewController を設定する。

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let mainStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
        titleViewController = mainStoryboard.instantiateViewController(withIdentifier: "TitleViewController") as? TitleViewController
        titleViewController!.containerViewController = self
        gameViewController = mainStoryboard.instantiateViewController(withIdentifier: "GameViewController") as? GameViewController
        gameViewController!.containerViewController = self

コンテナビューコントローラに子ビューコントローラを登録。
        self.addChildViewController(titleViewController!)
        self.addChildViewController(gameViewController!)
        titleViewController!.didMove(toParentViewController: self)
        gameViewController!.didMove(toParentViewController: self)

最初に表示するViewControllerを設定する。

        selectedViewController = titleViewController
        self.view.addSubview(selectedViewController!.view)

タイトル.png

タイトル画面でStartボタンが選択されるとコンテナViewControllerの画面遷移メソッドを呼ぶ。

class TitleViewController: UIViewController {
    var containerViewController: ContainerViewController?
    
    :
    
    @IBAction func startButtonTapped(_: AnyObject) {
        containerViewController!.toGameViewController()
    }
}

コンテナViewControllerのコードは以下のとおり。

    func toGameViewController() {
        transition(from: titleViewController!, to: gameViewController!, duration: 1.0, options: .transitionCrossDissolve, animations: nil, completion: { (finished: Bool) -> Void in self.selectedViewController = self.gameViewController })
    }

遷移したゲームViewControllerはSpriteKitを使ってゲーム・シーンを表示している。

class GameViewController: UIViewController {
    var containerViewController: ContainerViewController?
    var game: Game?

    override func viewDidLoad() {
        print(NSStringFromClass(type(of: self)), #function)
        super.viewDidLoad()
        
        game = Game()
        if let aGame = game {
            let scene = aGame.scene
            scene!.scaleMode = .aspectFill
            
            let skView = view as! SKView
            skView.presentScene(scene)
            skView.ignoresSiblingOrder = true
            skView.showsFPS = true
            skView.showsNodeCount = true
        }
    }

tvOSはiOSから派生しているということで、iOSと同様に記述できている。

ソースコード
GitHubからどうぞ。

https://github.com/murakami/workbook/tree/master/tvos/Pokopen - GitHub

関連情報
tvOSアプリケーションプログラミングガイド

tvOS Advent Calendar 2017

Cocoa Advent Calendar 2017

Cocoa勉強会 BUKURO.swift (connpass)

Cocoa勉強会 BUKURO.swift (ATND)

Cocoa勉強会 BUKURO.swift (Peatix)

【Cocoa練習帳】
http://www.bitz.co.jp/weblog/

http://ameblo.jp/bitz/(ミラー・サイト)

Qiita

6
2
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
6
2