我不想我的背景图像太模糊。难道没有一个属性来调整模糊强度吗?
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
blurEffect.???
let effectView = UIVisualEffectView(effect: blurEffect)
effectView.frame = backgroundAlbumCover.bounds
backgroundAlbumCover.addSubview(effectView)发布于 2015-07-22 05:18:17
调整模糊本身是不可能的..。但是,您可以调整模糊视图的可见度。可以通过多种方式做到这一点,目前我只能想到其中的三种:
第一选项:调整UIVisualEffectView实例的alpha值,例如:
effectView.alpha = 0.4f;第二个选项:在索引0处向effectView添加一个UIView实例,并调整这个UIView实例的alpha值。例如:
UIView *blurDilutionView = [[UIView alloc] initWithFrame: effectView.frame];
blurDilutionView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent: 0.5];
blurDilutionView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;//if not using AutoLayout
[effectView insertSubview:blurDilutionView atIndex:0];第三个选项:使用多个UIVisualEffectView实例(我还没有尝试过,更多的想法)。在每一个上加一个0.1f的α。UIVisualEffectView视图越多,总体外观就越模糊。再一次,我还没有尝试过这个选择!
更新:正如Axeva在评论中提到的,苹果建议不要调整alpha来改变模糊。所以,使用这些建议会给你带来潜在的危险。
发布于 2020-02-10 18:53:59
你可以用动画师的超优雅的方式来做。
(降低UIVisualEffectViewα不会影响模糊强度,所以我们必须使用动画)
用法非常简单,如:
let blurEffectView = BlurEffectView()
view.addSubview(blurEffectView)BlurEffectView实现:
class BlurEffectView: UIVisualEffectView {
var animator = UIViewPropertyAnimator(duration: 1, curve: .linear)
override func didMoveToSuperview() {
guard let superview = superview else { return }
backgroundColor = .clear
frame = superview.bounds //Or setup constraints instead
setupBlur()
}
private func setupBlur() {
animator.stopAnimation(true)
effect = nil
animator.addAnimations { [weak self] in
self?.effect = UIBlurEffect(style: .dark)
}
animator.fractionComplete = 0.1 //This is your blur intensity in range 0 - 1
}
deinit {
animator.stopAnimation(true)
}
}发布于 2018-07-01 13:54:20
有一次,我遇到了一个问题,想要创造一种比.light更暗,比.dark UIBlurEffect样式更轻的模糊效果。
要做到这一点,在背面放一个带有您需要的颜色和alpha的视图:
let pictureImageView = // Image that you need to blur
let backView = UIView(frame: pictureImageView.bounds)
backView.backgroundColor = UIColor(red: 100/255, green: 100/255, blue: 100/255, alpha: 0.3)
pictureImageView.addSubview(backView)
let blurEffect = UIBlurEffect(style: .light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = pictureImageView.bounds
pictureImageView.addSubview(blurEffectView)结果是什么样子:

有关更多细节,请查看此文章。
更新:显然还有另一种使用CIFilter(name: "CIGaussianBlur")实现模糊的好方法(甚至更好)。它允许制造“不透明”和模糊的优势远低于UIBlurEffect。
https://stackoverflow.com/questions/28140781
复制相似问题