简体   繁体   English

从View Controller模态切换到另一个控制器后,在View Controller上添加模糊效果

[英]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 (它涵盖了整个vc1view )。

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有模式显示(其中viewbackgroundColour设为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.

相关问题 在从一个视图控制器模式切换到另一个视图控制器后,视图全黑 - View is entirely black after modally switching from one view controller to another UIButton在第一次加载时不响应触摸,但是在从另一个视图控制器回退后可以工作 - UIButton doesn't respond to touch on first load, but works after segueing back from another view controller 从一个视图控制器“塞住”到另一个视图时遇到麻烦,得到奇怪的警告 - Having trouble 'segueing' from one view controller to another, getting weird warnings 将视图控制器导入另一个控制器进行segueing是否安全? - Is it safe to import a view controller to another controller for segueing? 与另一个控制器隔离后在控制器中处理响应 - Handing a response in a controller after segueing to another controller segueing到不同的视图控制器后动画停止 - Animation stops after segueing to different view controller 从一个视图控制器到另一个视图控制器(以模态呈现),反之亦然 - Make voice over focus from one view controller to another(presented modally) and vice versa 快速将模糊效果添加到登录视图控制器的背景 - adding blur effect to background of a log in view controller in swift 呈现具有模糊效果的视图控制器 - Presenting View Controller with Blur Effect iOS-模态呈现的VC,与以前的View Controller相比模糊 - iOS - Modally Present VC with blur over previous View Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM