在swift中为背景添加模糊效果 [英] Adding blur effect to background in swift
本文介绍了在swift中为背景添加模糊效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在设置背景图像来查看控制器。但我也想在这个背景下添加模糊效果。我该怎么做?
I am setting a background image to view controller. But also i want to add blur effect to this background. How can I do this?
我用以下代码设置背景:
I am setting background with following code:
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "testBg")!)
<我在互联网上找到模糊imageview如何实现这个到我的背景?
I found on internet for blur imageview how can i implement this to my background?
var darkBlur = UIBlurEffect(style: UIBlurEffectStyle.Dark)
// 2
var blurView = UIVisualEffectView(effect: darkBlur)
blurView.frame = imageView.bounds
// 3
imageView.addSubview(blurView)
推荐答案
我已经测试了这段代码并且工作正常:
I have tested this code and it's working fine:
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
view.addSubview(blurEffectView)
对于Sw ift 3.0:
For Swift 3.0:
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(blurEffectView)
在这里你可以看到结果:
Here you can see result:
或者您可以使用此库:
https://github.com/FlexMonkey/Blurable
这篇关于在swift中为背景添加模糊效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文