UIVisualEffectView不会模糊它是SKView超级视图 [英] UIVisualEffectView doesn't blur it's SKView superview

查看:174
本文介绍了UIVisualEffectView不会模糊它是SKView超级视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写SpriteKit游戏,但在SKView上遇到视图模糊的问题.游戏暂停时,它应该从右侧滑动,并且应该模糊其父视图(SKView)的内容,就像iOS 7中的控制中心面板一样.这是所需的外观:

I'm writing a SpriteKit game and faced a problem with blurred view, which lies on the SKView. It is supposed to slide from the right when game is paused and it should blur the content of it's parent view (SKView) just like control center panel in iOS 7. Here is the desired appearance:

我实际上得到的是:

实际上,左侧视图并非全黑,您可以看到超级视图中的高光在几乎不透明的子视图中略微挣扎,但没有应用模糊.是iOS 8的错误/功能,还是我的错误/误解

In fact the view on the left is not totally black, you can see how highlights from the superview are slightly struggling through almost opaque subview, but no blur is applied. Is it an iOS 8 bug/feature, or is it my mistake/misunderstanding

这是我的UIVisualEffectView子类的基本知识:

Here is my UIVisualEffectView subclass's essensials:

class OptionsView: UIVisualEffectView {
//...
    init(size: CGSize) {
        buttons = [UIButton]()
        super.init(effect: UIBlurEffect(style: .Dark))
        frame = CGRectMake(-size.width, 0, size.width, size.height)
        addButtons()
        clipsToBounds = true
    }
    func show() {
        UIView.animateWithDuration(0.3, animations: {
            self.frame.origin.x = 0
        })
    }
    func hide() {
        UIView.animateWithDuration(0.3, animations: {
            self.frame.origin.x = -self.frame.size.width
        })
    }

然后在GameScene类中:

Then in GameScene class:

在初始化程序中:

optionsView = OptionsView(size: CGSizeMake(130, size.height))

在didMoveToView(视图:SKView)中:

in didMoveToView(view: SKView):

view.addSubview(optionsView)

在按下暂停按钮时:

self.optionsView.show()

P.S.尽管我知道另外两种实现模糊视图的方法,但我认为这是最简单的方法,因为我的应用仅支持iOS8

P.S. Though I know two another ways to implement blur view, I thought this one was the easiest, since my app is going to support iOS8 only

  1. 从Superview中渲染模糊的静态图像-> 将UIImageView放在optionsView上,并带有clipsToBounds = true-> 为UIImageView位置设置动画,同时为optionsView位置设置动画,以使模糊相对于超级视图保持相对不变

  1. Render a blurred static image from superview -> put UIImageView on the OptionsView, with clipsToBounds = true -> animate UIImageView position while animating optionsView position, so that blur stays still relatively to the superview

忘记UIView,UIVisualEffectView和UIBlurView,将SKEffectNode与SKCropNode一起使用.

Forget about UIView, UIVisualEffectView and UIBlurView and use SKEffectNode together with SKCropNode.

推荐答案

好,我设法使用SKEffectNode而不是UIVisualEffectView来获得所需的效果. 这是面临相同问题的人的代码

Ok, I have managed to get the desired effect using SKEffectNode instead of UIVisualEffectView. Here is the code for someone facing the same issue

class BlurCropNode: SKCropNode {
    var blurNode: BlurNode
    var size: CGSize
    init(size: CGSize) {
        self.size = size
        blurNode = BlurNode(radius: 10)
        super.init()
        addChild(blurNode)
        let mask = SKSpriteNode (color: UIColor.blackColor(), size: size)
        mask.anchorPoint = CGPoint.zeroPoint
        maskNode = mask
    }
}

class BlurNode: SKEffectNode {
    var sprite: SKSpriteNode
    var texture: SKTexture {
        get { return sprite.texture }
        set {
            sprite.texture = newValue
            let scale = UIScreen.mainScreen().scale
            let textureSize = newValue.size()
            sprite.size = CGSizeMake(textureSize.width/scale, textureSize.height/scale)
        }
    }
    init(radius: CGFloat) {
        sprite = SKSpriteNode()
        super.init()
        sprite.anchorPoint = CGPointMake(0, 0)
        addChild(sprite)
        filter = CIFilter(name: "CIGaussianBlur", withInputParameters: ["inputRadius": radius])
        shouldEnableEffects = true
        shouldRasterize = true
    }
}

结果:

尽管有几个问题

  1. 在SKEffectNode上将其裁剪不起作用,直到它的shouldRasterize属性设置为true为止.我使整个屏幕变得模糊.所以我仍然不知道如何正确地实现实时模糊.

  1. Cropping doesn't work with SKEffectNode until it's shouldRasterize property is set to true. I get the whole screen blurred. So I still don't know how to properly implement realtime blur.

BlurCropNode上的动画不够流畅.由于捕获纹理并将其设置为effectNode的sprite子级,因此开始时会有滞后.甚至dispatch_async也无济于事.

Animation on the BlurCropNode is not smooth enough. There is a lag at the beginning because of capturing texture and setting it to the effectNode's sprite child. Even dispatch_async doesn't help.

如果能帮助解决至少一个问题,将不胜感激

It would be very much appreciated if anyone could help to solve at least one of the problems

这篇关于UIVisualEffectView不会模糊它是SKView超级视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆