[英]Adding a blur effect on a View Controller after segueing from it to another one modally
I have a view controller( vc1
) that contains a button. 我有一个包含一个按钮的视图控制器( vc1
)。 That button shows another view controller ( vc2
) modally. 该按钮以模态显示另一个视图控制器( vc2
)。 presentation style
of the segue is set to over current context
. 剧情的presentation style
被设置为over current context
。
I've also created a UIVisualEffectView
on a vc1 programatically (it covers the whole vc1
's view
). 我还以编程方式在UIVisualEffectView
上创建了UIVisualEffectView
(它涵盖了整个vc1
的view
)。
Now, I need to set a visual effect's effect to UIBlurEffect
, but after the vc2
is shown modally (which view
's backgroundColour
is set to clear
, so vc1
is still visible behind vc2
). 现在,我需要一个视觉效果的效果设置为UIBlurEffect
,但之后vc2
有模式显示(其中view
的backgroundColour
设为clear
,因此vc1
仍落后可见vc2
)。
Is it possible to achieve without having to write a custom view controller transition and if so, how this could be done? 是否有可能无需编写自定义视图控制器转换就可以实现?如果是,该怎么做? If you know how to do this, I would appreciate any help or suggestions. 如果您知道该怎么做,我们将不胜感激。
Blur transition does not come by default in iOS. 在iOS中,默认情况下不会进行模糊过渡。 So you need to customise code. 因此,您需要自定义代码。
A simple solution would be to add a subview : 一个简单的解决方案是添加一个子视图:
Add the following code while transitioning to vc2 : 过渡到vc2时添加以下代码:
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
blurEffectView = UIVisualEffectView(effect: blurEffect) // Global variable
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.view.addSubview(blurEffectView)
NotificationCenter.default.addObserver(self, selector: #selector(self.removeBlurView), name: NSNotification.Name(rawValue: "removeBlurView"), object: nil)
Function for removing blur view (in vc1) : 消除模糊视图的功能(在vc1中):
func removeBlurView()
{
NotificationCenter.default.removeObserver(self)
blurEffectView.removeFromSuperview()
}
Add following in vc2 when you dismiss vc2 : 关闭vc2时,在vc2中添加以下内容:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "removeBlurView"), object: nil)
There are other ways to accomplish this as stated in Ray's tutorial and a closely related tutorial 如Ray的教程和一个紧密相关的教程所述,还有其他方法可以实现此目的
You can use child ViewController mechanism. 您可以使用子级ViewController机制。
let vc2 = VC2()
vc1.addChildViewController(vc2)
vc2.view.frame = vc1.view.frame
vc1.view.addSubview(vc2.view)
vc2.didMove(toParentViewController: vc1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.