如何淡入UIVisualEffectView和/或UIBlurEffect进出?

我想用UIBlurEffect淡入一个UIVisualEffectsView:

var blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark) var blurEffectView = UIVisualEffectView() blurEffectView = UIVisualEffectView(effect: blurEffect) 

我在一个UIButton调用的函数中使用普通animation淡入淡出,但.alpha = 0hidden = true

 UIView.animateWithDuration(1, delay:0, options: .CurveEaseOut, animations: { self.blurEffectView.alpha = 1 }, completion: { finished in self.blurEffectView.hidden = false }) 

现在,在两个方向上的衰落都可以工作,但是当淡出时它会给我一个错误:要求<UIVisualEffectView 0x7fdf5bcb6e80> is being asked to animate its opacity. This will cause the effect to appear broken until opacity returns to 1. <UIVisualEffectView 0x7fdf5bcb6e80> is being asked to animate its opacity. This will cause the effect to appear broken until opacity returns to 1.

如何成功淡入淡出UIVisualEffectsView而不破坏它,并有淡入淡出的过渡?

注意

  • 我试图把UIVisualEffectView放到一个UIView中,并淡化那个,没有成功

我认为这是iOS9中的新function,但现在可以在animation块中设置UIVisualEffectView的效果:

 let overlay = UIVisualEffectView() // Put it somewhere, give it a frame... UIView.animate(withDuration: 0.5) { overlay.effect = UIBlurEffect(style: .light) } 

将其设置nil以删除。

苹果文件 (目前)说…

使用UIVisualEffectView类时,请避免小于1的alpha值。

在视觉效果视图或其任何超级视图上将alpha设置为小于1会导致很多效果看起来不正确或根本不显示。

我相信这里缺less一些重要的背景

我build议意图是为了避免持久视图的alpha值小于1。 在我看来,这不适用于视图的animation。

我的观点 – 我build议alpha值小于1的animation是可以接受的。

terminal消息指出:

UIVisualEffectView正在被要求animation的不透明度。 这将导致效果显示中断,直到不透明度返回到1。

仔细阅读,效果似乎会被打破。 我在这里提出两点:

  • 明显的突破对于持续不变的观点来说确实很重要,
  • alpha值小于1的持久/不变UIVisualEffect视图不会按照Appledevise/devise的方式呈现; 和
  • 在terminal的消息不是一个错误,只是一个警告。

为了扩展@ jrturton的答案,帮助我解决了我的问题,我添加了…

要淡出UIVisualEffect使用以下内容:

 UIView.animateWithDuration(1.0, animations: { // EITHER... self.blurEffectView.effect = UIBlurEffect(nil) // OR... self.blurEffectView.alpha = 0 }, completion: { (finished: Bool) -> Void in self.blurEffectView.removeFromSuperview() } ) 

我成功地使用了这两种方法:将effect属性设置nil ,并将alpha属性设置为0

请注意,将effect设置为nil会在animation结束时创build“漂亮的闪光灯”(为了更好的描述),而将alpha设置为0则会创build平滑过渡。

(让我知道任何语法错误…我写在OBJ – C。)

只是一个解决方法 – 将UIVisualEffectView放入一个容器视图并更改该容器的alpha属性。 这种方法在iOS 9上完全适用于我。似乎它不再适用于iOS 10。

这里是我最终的解决scheme,它使用Swift 3在iOS10和早期版本上都可以工作

 extension UIVisualEffectView { func fadeInEffect(_ style:UIBlurEffectStyle = .light, withDuration duration: TimeInterval = 1.0) { if #available(iOS 10.0, *) { let animator = UIViewPropertyAnimator(duration: duration, curve: .easeIn) { self.effect = UIBlurEffect(style: style) } animator.startAnimation() }else { // Fallback on earlier versions UIView.animate(withDuration: duration) { self.effect = UIBlurEffect(style: style) } } } func fadeOutEffect(withDuration duration: TimeInterval = 1.0) { if #available(iOS 10.0, *) { let animator = UIViewPropertyAnimator(duration: duration, curve: .linear) { self.effect = nil } animator.startAnimation() animator.fractionComplete = 1 }else { // Fallback on earlier versions UIView.animate(withDuration: duration) { self.effect = nil } } } } 

您也可以查看这个要点来查找示例用法。

您可以拍摄静态底层视图的快照,并淡入淡出,而不触及模糊视图的不透明度。 假设一个伊娃的blurView:

 func addBlur() { guard let blurEffectView = blurEffectView else { return } //snapShot = UIScreen.mainScreen().snapshotViewAfterScreenUpdates(false) let snapShot = self.view.snapshotViewAfterScreenUpdates(false) view.addSubview(blurEffectView) view.addSubview(snapShot) UIView.animateWithDuration(0.25, animations: { snapShot.alpha = 0.0 }, completion: { (finished: Bool) -> Void in snapShot.removeFromSuperview() } ) } func removeBlur() { guard let blurEffectView = blurEffectView else { return } let snapShot = self.view.snapshotViewAfterScreenUpdates(false) snapShot.alpha = 0.0 view.addSubview(snapShot) UIView.animateWithDuration(0.25, animations: { snapShot.alpha = 1.0 }, completion: { (finished: Bool) -> Void in blurEffectView.removeFromSuperview() snapShot.removeFromSuperview() } ) } 

除了控制台中的警告外,您可以更改视觉效果视图的Alpha,而不会出现任何问题。 视图可能只是部分透明的,而不是模糊的。 但是这通常不是一个问题,如果你只是在animation过程中改变alpha。

你的应用程序不会崩溃或被拒绝。 在真实设备(或八个)上testing。 如果你对它的外观和performance很满意,那很好。 苹果公司只是警告你,它可能看起来不像一个alpha值为1的视觉效果视图。

UIVisualEffectView的alpha必须是1.我认为你可以通过设置背景颜色的alpha来达到效果。

资料来源: https : //developer.apple.com/library/ios/documentation/UIKit/Reference/UIVisualEffectView/index.html

我做了一个uiview,alpha是0,并添加blurview作为子视图。 所以我可以用animation来隐藏/显示或倒angular。

如果你想淡入你的UIVisualEffectView – 对于ios10使用UIViewPropertyAnimator

  UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:nil]; blurEffectView.frame = self.view.frame; blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; UIView *blackUIView = [[UIView alloc]initWithFrame:self.view.frame]; [bacgroundImageView addSubview:blackUIView]; [blackUIView addSubview:blurEffectView]; UIViewPropertyAnimator *animator = [[UIViewPropertyAnimator alloc] initWithDuration:4.f curve:UIViewAnimationCurveLinear animations:^{ [blurEffectView setEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]]; }]; 

那么你可以设定百分比

 [animator setFractionComplete:percent]; 

对于ios9,你可以使用alpha组件

我结束了以下解决scheme,使用单独的animation为UIVisualEffectView和内容。 我使用viewWithTag()方法来获取对UIVisualEffectView内的UIViewUIVisualEffectView

 let blurEffectView = UIVisualEffectView() // Fade in UIView.animateWithDuration(1) { self.blurEffectView.effect = UIBlurEffect(style: .Light) } UIView.animateWithDuration(1) { self.blurEffectView.viewWithTag(1)?.alpha = 1 } // Fade out UIView.animateWithDuration(1) { self.blurEffectView.effect = nil } UIView.animateWithDuration(1) { self.blurEffectView.viewWithTag(1)?.alpha = 0 } 

我更喜欢单个animation改变阿尔法,但这避免了错误,似乎也工作。

我只是有这个问题,我得到的方式是UIVisualEffectsView在UIView,并animationUIView的alpha。

这个效果很好,只是一旦alpha变成1.0以下,就变成了纯白色,看起来很刺耳。 为了解决这个问题,你必须设置UIView的图层属性containerView.layer.allowsGroupOpacity = false ,这将防止它闪烁白色。

现在,您可以使用它的alpha属性对包含视觉效果视图和任何其他子视图的UIView进行animation处理/淡出,而不必担心任何graphics故障或logging警告消息。

 _visualEffectView.contentView.alpha = 0; 

要更改UIVisualEffectView的alpha值,您应该更改_visualEffectView的contentView。如果更改_visualEffectView的alpha值,您将得到

 <UIVisualEffectView 0x7ff7bb54b490> is being asked to animate its opacity. This will cause the effect to appear broken until opacity returns to 1. 

通常,我只想在屏幕上显示视图控制器并想模糊呈现视图控制器时模糊。 这是一个扩展,它将blur()和unblur()添加到视图控制器,以便于:

 extension UIViewController { func blur() { // Blur out the current view let blurView = UIVisualEffectView(frame: self.view.frame) self.view.addSubview(blurView) UIView.animate(withDuration:0.25) { blurView.effect = UIBlurEffect(style: .light) } } func unblur() { for childView in view.subviews { guard let effectView = childView as? UIVisualEffectView else { continue } UIView.animate(withDuration: 0.25, animations: { effectView.effect = nil }) { didFinish in effectView.removeFromSuperview() } } } } 

当然,通过让用户select效果风格,修改持续时间,在animation完成时调用某些内容,在blur()中添加视觉效果视图以确保它是唯一一个在unblur )等等,但是到目前为止我还没有发现需要做这些事情,因为这往往是一种“失火而忘记”的操作。

Interesting Posts