LoginSignup
6
6

More than 5 years have passed since last update.

SpriteKitを試してみる8 画面遷移

Posted at

iphonePlay_20150426.gif

spriteKitでの画面遷移は簡単です。

GameScene.swift

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        /* Called when a touch begins */

        for touch in (touches as! Set<UITouch>) {
            let nextScene = NextScene()
            nextScene.size = self.size
             let transition = SKTransition.crossFadeWithDuration(2)

            self.view?.presentScene(nextScene, transition: transition)

            }
    }

NextScene.swift

import Foundation
import SpriteKit

class NextScene: SKScene {
    override func didMoveToView(view: SKView) {
        /* Setup your scene here */
        self.backgroundColor = UIColor.blueColor()


        let myLabel = SKLabelNode(fontNamed:"Chalkduster")
        myLabel.text = "Goodbye, World!";
        myLabel.fontSize = 65;
        myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));

        self.addChild(myLabel)
    }
}

これ以外の画面遷移の種類は以下のページが大変参考になります。ソースはObjective-Cですが基本は変わってないと思います。

[iOS 7] Sprite Kit の画面遷移アニメーションまとめ

で、次にSKTransitionの種類一つでCIFilterを使ったやつのサンプルを。上のリンクではサンプルがないので。

iphonePlay_2015042602.gif

GameScene.swift

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        /* Called when a touch begins */

        for touch in (touches as! Set<UITouch>) {
            let nextScene = NextScene()
            nextScene.size = self.size
             let ciFilter:CIFilter = CIFilter(name: "CISwipeTransition")
            ciFilter.setDefaults()
            let transition = SKTransition(CIFilter: ciFilter, duration: 2)

            self.view?.presentScene(nextScene, transition: transition)

            }
    }

このCIFilterを使うってところで躓いたので注意点を。なぜ躓いたかというと

CIFilterに当てはまるなら何でも使えるというわけではない

という事にしばらく気付かなかったので。
例えば当初はCISepiaToneを使おうとしました。

    let ciFilter:CIFilter = CIFilter(name: "CISepiaTone")
    ciFilter.setValue(0.8, forKey: "inputIntensity")

しかし、これはコンパイルエラーにはならないが、実行時にNSUnknownKeyExceptionになる。具体的にはこのエラーが出ます。

this class is not key value coding-compliant for the key inputTargetImage.'

当初はセットし忘れかと思って追加でsetValueしたりしたがまったく解決しない。それでリファレンスを見直してみたらどうもパラメータにinputImageとinputTargetImageを使ってるやつしか使えないようです。そのルールに照らせばCISepiaToneのパラメータはinputImageとinputIntensityしかない為、エラーになるのは道理という事になります。

つまり SKTransitionのCIFilterにセット出来るのはinputImage, inputTargetImageをパラメータにもつものだけ という事になります。

ではどのフィルターなら可能なのかという事についてはリファレンスを参照して頂きたい。ただパラメータの条件を満たしていても、iOSで使えないものもちょいちょいあるのでご注意を。
他にもフィルターの種類によってはパラメータの設定が追加で必要なのもあるかもしれません。全部を試してはいないのではっきりとはしませんが。

Core Image Filter Reference

6
6
3

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
6