如何在Swift 2中模糊场景

时间:2016-02-11 04:55:37

标签: ios sprite-kit swift2

当我暂停游戏时,我试图模糊一个场景并且我跟随example,但我无法在Swift 2.0中解决这个问题。

许多教程说只是截取屏幕截图,然后将屏幕截图显示为模糊,但我认为这不是一个好主意,我想在没有屏幕截图的情况下模糊视图。

这是我的尝试:

func createlayers() {
    let node = SKEffectNode()
    node.shouldEnableEffects = false
    let filter: CIFilter = CIFilter(name: "CIGaussianBlur", withInputParameters: ["inputRadius" : NSNumber(double:1.0)])!
    node.filter = filter
}

func blurWithCompletion() {
    let duration: CGFloat = 0.5
    scene!.shouldRasterize = true
    scene!.shouldEnableEffects = true
    scene!.runAction(SKAction.customActionWithDuration(0.5, actionBlock: { (node: SKNode, elapsedTime: CGFloat) in
        let radius = (elapsedTime/duration)*10.0
        (node as? SKEffectNode)!.filter!.setValue(radius, forKey: "inputRadius")

    }))
}

func pauseGame()
{
    self.blurWithCompletion()
    self.view!.paused = true 

}

我得到了#34;致命错误:在打开一个可选值&#34时意外地发现了nil;

1 个答案:

答案 0 :(得分:2)

不需要创建图层方法。 使用此更新的blurWithCompletion方法:

    func blurWithCompletion() {
    let duration: CGFloat = 0.5
    let filter: CIFilter = CIFilter(name: "CIGaussianBlur", withInputParameters: ["inputRadius" : NSNumber(double:1.0)])!
    scene!.filter = filter
    scene!.shouldRasterize = true
    scene!.shouldEnableEffects = true
    scene!.runAction(SKAction.customActionWithDuration(0.5, actionBlock: { (node: SKNode, elapsedTime: CGFloat) in
        let radius = (elapsedTime/duration)*10.0
        (node as? SKEffectNode)!.filter!.setValue(radius, forKey: "inputRadius")

    }))
}