OStack程序员社区-中国程序员成长平台

标题: ios - 如何在 Swift 2 中模糊场景 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:27
标题: ios - 如何在 Swift 2 中模糊场景

我在暂停游戏时试图模糊一个场景并且我正在关注 example但我无法在 Swift 2.0 中解决。

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

这是我的尝试:

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 

}

我收到“ fatal error :在展开可选值时意外发现 nil”



Best Answer-推荐答案


不需要创建图层方法。 使用这个更新的 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")

    }))
}

关于ios - 如何在 Swift 2 中模糊场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35331053/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4