ios - 如何在 Swift 2 中模糊场景
<p><p>我在暂停游戏时试图模糊一个场景并且我正在关注 <a href="https://stackoverflow.com/questions/22490818/how-do-i-blur-a-scene-in-spritekit" rel="noreferrer noopener nofollow">example</a>但我无法在 Swift 2.0 中解决。</p>
<p>很多教程说只截取屏幕截图,然后将该屏幕截图显示为模糊,但我认为这不是一个好主意,我想在没有屏幕截图的情况下模糊 View 。 </p>
<p>这是我的尝试:</p>
<pre><code>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
}
</code></pre>
<p>我收到“ fatal error :在展开可选值时意外发现 nil”</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>不需要创建图层方法。
使用这个更新的 blurWithCompletion 方法:</p>
<pre><code> 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")
}))
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何在 Swift 2 中模糊场景,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/35331053/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/35331053/
</a>
</p>
页:
[1]