简体   繁体   中英

use UIVisualEffectView to create a blur view, correct on simulator but not on iphone & ipad

The goal: create a blur view in app.

The code I use:

func createBlurBackgroundView() 
{
    if !UIAccessibilityIsReduceTransparencyEnabled() 
    {
        if blurredSubView == nil || blurredSubView.superview == nil 
        {
            print("Create blurred background view")
            self.backgroundColor = UIColor.clearColor()
            let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
            blurredSubView = UIVisualEffectView(effect: blurEffect)
            blurredSubView.frame = self.bounds
            self.insertSubview(blurredSubView, atIndex: 0)
        }
    }
    else 
    {
        print("Transparency disabled!! no blur view")
    }
}

The result: everything works fine on the simulator:

在此输入图像描述

But when I ran it on the iphone and ipads, it looks like: 在此输入图像描述

PLEASE NOTE I DIDN'T CHANGE THE "REDUCE TRANSPARENCY" SETTINGS!

Then when I want to take a snapshot of this black background without blur, guess what?! in the photo stream, I saw exactly the correct blur view picture...

Also, when I double clicked home button, and look at the multi-task interface, I saw the blur view effect!

在此输入图像描述

More Info: I use iphone6s, ipad air2, both iOS 9.3.1 Xcode version 7.3

I'm so tired on trying to solve this problem, I tried other methods like take snapshot image and then apply blur effect on the image, but has other bugs and other CONs

UIVisualEffectView does not work with SpriteKit . I don't know what they do differently in the back, if someone knows please feel free to edit the answer. My guess is that the underlying implementation use different APIs that don't work togheter. The simulator does all kinds of tricks to simulate the actual device so they might use something different in the back than the real devices and that's why it does work on a simulator.

在代码中删除此行

self.backgroundColor = UIColor.clearColor()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM