我不希望我的背景图像过于眩晕.是否有调整模糊强度的属性?
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
blurEffect.???
let effectView = UIVisualEffectView(effect: blurEffect)
effectView.frame = backgroundAlbumCover.bounds
backgroundAlbumCover.addSubview(effectView)
Run Code Online (Sandbox Code Playgroud)
bod*_*ich 19
你可以用动画师以超级优雅的方式做到这一点
(减少 UIVisualEffectView alpha 不会影响模糊强度,所以我们必须使用 animator)
用法很简单:
let blurEffectView = BlurEffectView()
view.addSubview(blurEffectView)
Run Code Online (Sandbox Code Playgroud)
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)
}
}
Run Code Online (Sandbox Code Playgroud)
pni*_*zle 13
无法调整模糊本身......但是,您可以调整模糊视图的可见度.这可以通过多种方式完成,目前我只能想到其中的三种方式:
第一个选项:调整UIVisualEffectView实例的alpha,例如:
effectView.alpha = 0.4f;
Run Code Online (Sandbox Code Playgroud)
第二个选项:在Index 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];
Run Code Online (Sandbox Code Playgroud)
第三个选项:使用多个UIVisualEffectView实例(我还没有尝试过这个,更多的想法).每个都应用0.1f的alpha值.UIVisualEffectView视图越多,整体外观就越模糊.再一次,我还没有尝试过这个选项!
有一次我遇到了创建.light比.darkUIBlurEffect 样式更暗和更亮的模糊效果的问题。
要实现这一点,请在背面放置一个带有您需要的颜色和 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)
Run Code Online (Sandbox Code Playgroud)
结果如何:
有关更多详细信息,请查看这篇文章。
更新:显然还有另一种不错(甚至更好)的方式来使用CIFilter(name: "CIGaussianBlur"). 它允许使“不透明度”和模糊的强度远低于UIBlurEffect.