较less模糊与视觉效果视图与模糊?

标题几乎要求所有…

我正在玩与iOS8 Visual Effect View with Blur 。 它是通过显示用户可选背景照片的UIImageView 。 放置在contentView本身的视图是我自己的自定义视图,显示了一种graphics/日历。 大部分的日历图是透明的。 它适用于背后的照片是非常沉重的。 我想让更多的细节泄露。 但是苹果似乎只给出了三个jar头价值:

 typedef enum { UIBlurEffectStyleExtraLight, UIBlurEffectStyleLight, UIBlurEffectStyleDark } UIBlurEffectStyle; 

我已经用UIVisualEffectView的不同alpha和背景颜色(即使文档警告反对),但这并没有做任何事情,但更糟的是。

模糊效果的风格影响图像的亮度级别,而不是应用的模糊量。

在这里输入图像说明

不幸的是,虽然苹果显然有能力以编程方式控制模糊量 – 尝试在启动UIBlurEffect缓慢拖动以观看Spotlight模糊转换 – 我没有看到任何公共API将模糊量传递给UIBlurEffect

这篇文章声称,调整背景颜色阿尔法将驱动模糊量。 这是值得一试,但我不知道在哪里logging: 如何淡入UIVisualEffectView和/或UIBlurEffect进出?

可惜的是,苹果没有提供任何模糊效果的选项。 但是这个解决方法对我来说很有效 – animation模糊效果并在完成之前暂停。

 func blurEffectView(enable enable: Bool) { let enabled = self.blurView.effect != nil guard enable != enabled else { return } switch enable { case true: let blurEffect = UIBlurEffect(style: .ExtraLight) UIView.animateWithDuration(1.5) { self.blurView.effect = blurEffect } self.blurView.pauseAnimation(delay: 0.3) case false: self.blurView.resumeAnimation() UIView.animateWithDuration(0.1) { self.blurView.effect = nil } } } 

和UIView扩展(暂停)和恢复视图的animation

 extension UIView { public func pauseAnimation(delay delay: Double) { let time = delay + CFAbsoluteTimeGetCurrent() let timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, time, 0, 0, 0, { timer in let layer = self.layer let pausedTime = layer.convertTime(CACurrentMediaTime(), fromLayer: nil) layer.speed = 0.0 layer.timeOffset = pausedTime }) CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes) } public func resumeAnimation() { let pausedTime = layer.timeOffset layer.speed = 1.0 layer.timeOffset = 0.0 layer.beginTime = layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pausedTime } } 

这对我有用。

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

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

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

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

 addBlurArea(self.view.frame, style: UIBlurEffectStyle.Dark) 

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

添加一个BlurEffectView到视图的alpha <1的视图

 func addBlurEffectView() -> Void { if !UIAccessibilityIsReduceTransparencyEnabled() { let viewContainer = UIView() viewContainer.frame = self.view.bounds viewContainer.alpha = 0.5 let blurEffect = UIBlurEffect(style: .dark) let blurEffectView = UIVisualEffectView(effect: blurEffect) blurEffectView.layer.zPosition = -0.5; blurEffectView.frame = self.view.bounds; blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] viewContainer.addSubview(blurEffectView) self.view.addSubview(viewContainer) self.view.sendSubview(toBack: viewContainer) } } 

是的你可以。

下面是一个带有模糊效果的UIImageView的例子。 请记住将图像添加到UIImageView。

调整模糊量blurEffectView.alpha = 0.8(从0到1)

 import UIKit class BlurEffectImageView: UIImageView { override func awakeFromNib() { addBlurEffect() super.awakeFromNib() } private func addBlurEffect(){ let blurEffect = UIBlurEffect(style: .light) let blurEffectView = UIVisualEffectView(effect: blurEffect) blurEffectView.alpha = 0.8 blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] blurEffectView.translatesAutoresizingMaskIntoConstraints = false addSubview(blurEffectView) NSLayoutConstraint(item: blurEffectView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0).isActive = true NSLayoutConstraint(item: blurEffectView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0).isActive = true NSLayoutConstraint(item: blurEffectView, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 1.0, constant: 0).isActive = true NSLayoutConstraint(item: blurEffectView, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 1.0, constant: 0).isActive = true } 

}

这个答案是基于Mitja Semolic早期的优秀答案 。 我已经将它转换为swift 3,添加了关于在coments中发生了什么的解释,使其成为UIViewController的扩展,以便任何VC可以随意调用它,添加一个未经视图的视图来显示select性应用程序,并添加一个完成块调用视图控制器可以完成模糊任务。

  import UIKit //This extension implements a blur to the entire screen, puts up a HUD and then waits and dismisses the view. extension UIViewController { func blurAndShowHUD(duration: Double, message: String, completion: @escaping () -> Void) { //with completion block //1. Create the blur effect & the view it will occupy let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light) let blurEffectView = UIVisualEffectView()//(effect: blurEffect) blurEffectView.frame = self.view.bounds blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] //2. Add the effect view to the main view self.view.addSubview(blurEffectView) //3. Create the hud and add it to the main view let hud = HudView.getHUD(view: self.view, withMessage: message) self.view.addSubview(hud) //4. Begin applying the blur effect to the effect view UIView.animate(withDuration: 0.01, animations: { blurEffectView.effect = blurEffect }) //5. Halt the blur effects application to achieve the desired blur radius self.view.pauseAnimationsInThisView(delay: 0.004) //6. Remove the view (& the HUD) after the completion of the duration DispatchQueue.main.asyncAfter(deadline: .now() + duration) { blurEffectView.removeFromSuperview() hud.removeFromSuperview() self.view.resumeAnimationsInThisView() completion() } } } extension UIView { public func pauseAnimationsInThisView(delay: Double) { let time = delay + CFAbsoluteTimeGetCurrent() let timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, time, 0, 0, 0, { timer in let layer = self.layer let pausedTime = layer.convertTime(CACurrentMediaTime(), from: nil) layer.speed = 0.0 layer.timeOffset = pausedTime }) CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, CFRunLoopMode.commonModes) } public func resumeAnimationsInThisView() { let pausedTime = layer.timeOffset layer.speed = 1.0 layer.timeOffset = 0.0 layer.beginTime = layer.convertTime(CACurrentMediaTime(), from: nil) - pausedTime } } 

我已经确认它适用于iOS 10.3.1和iOS 11