如何设置UIBlurEffectStyle.Light的BlurRadius

我想知道如何设置iOS新UIBlurEffectStyle.Light的半径/模糊系数? 我在文档中找不到任何东西。 但我希望它看起来类似于经典的“UIImage + ImageEffects.h”模糊效果。

谢谢,

required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) let blur:UIBlurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light) let effectView:UIVisualEffectView = UIVisualEffectView (effect: blur) effectView.frame = frame addSubview(effectView) } 

您可以更改将模糊效果添加到的UIVisualEffectView的Alpha。

 let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light) let blurEffectView = UIVisualEffectView(effect: blurEffect) blurEffectView.alpha = 0.5 blurEffectView.frame = self.view.bounds self.view.addSubview(blurEffectView) 

这不是一个真正的解决scheme,因为它实际上并没有改变模糊的半径,但是我发现它完成了很less的工作。

虽然这是一个黑客,可能它不会被应用程序商店接受,但仍然有可能。 你必须像这样UIBlurEffect

 #import <objc/runtime.h> @interface UIBlurEffect (Protected) @property (nonatomic, readonly) id effectSettings; @end @interface MyBlurEffect : UIBlurEffect @end @implementation MyBlurEffect + (instancetype)effectWithStyle:(UIBlurEffectStyle)style { id result = [super effectWithStyle:style]; object_setClass(result, self); return result; } - (id)effectSettings { id settings = [super effectSettings]; [settings setValue:@50 forKey:@"blurRadius"]; return settings; } - (id)copyWithZone:(NSZone*)zone { id result = [super copyWithZone:zone]; object_setClass(result, [self class]); return result; } @end 

这里模糊半径设置为50.您可以将50更改为您需要的任何值。

然后,在为UIBlurEffect创build效果时,只需使用MyBlurEffect类而不是UIVisualEffectView

最近开发的Bluuur库可以dynamic地改变UIVisualEffectsView模糊半径,而无需使用任何私有API: https : //github.com/ML-Works/Bluuur

它使用设置effect暂停animation来实现模糊半径的变化。 基于这个要点的解决scheme: https : //gist.github.com/n00neimp0rtant/27829d87118d984232a4

主要思想是:

 // Freeze animation blurView.layer.speed = 0; blurView.effect = nil; [UIView animateWithDuration:1.0 animations:^{ blurView.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; }]; // Set animation progress from 0 to 1 blurView.layer.timeOffset = 0.5; 

更新:

苹果公司在iOS 10中引入了UIViewPropertyAnimator类。这就是我们所需要的,用于animation.effect属性。 Hope社区将能够将此function支持到以前的iOS版本。

恐怕目前没有这样的api。 按照苹果的做法,新function总是带有限制,function将逐渐显现出来。 也许这将是可能的iOS 9或10 …

这对我有用。

在添加到我的视图之前,我把UIVisualEffectView放在一个UIView

我使这个function更容易使用。 您可以使用此function在视图中模糊任何区域。

 func addBlurArea(area: CGRect) { let effect = UIBlurEffect(style: UIBlurEffectStyle.Dark) let blurView = UIVisualEffectView(effect: effect) blurView.frame = CGRect(x: 0, y: 0, width: area.width, height: area.height) let container = UIView(frame: area) container.alpha = 0.8 container.addSubview(blurView) self.view.insertSubview(container, atIndex: 1) } 

例如,您可以通过调用以下命令来模糊所有视图:

 addBlurArea(self.view.frame) 

您可以将Dark更改为所需的模糊样式,然后将0.8更改为所需的Alpha值

目前我没有find任何解决scheme。
顺便说一句,你可以添加一些黑客手段,让模糊蒙版减less“模糊”,这样:

 let blurView = .. // here create blur view as usually if let blurSubviews = self.blurView?.subviews { for subview in blurSubviews { if let filterView = NSClassFromString("_UIVisualEffectFilterView") { if subview.isKindOfClass(filterView) { subview.hidden = true } } } } 

如果您想要完成与iOS Spotlightsearch相同的行为,只需更改UIVisualEffectView的Alpha值(在iOS9模拟器上testing)