簡體   English   中英

從View Controller模態切換到另一個控制器后,在View Controller上添加模糊效果

[英]Adding a blur effect on a View Controller after segueing from it to another one modally

我有一個包含一個按鈕的視圖控制器( vc1 )。 該按鈕以模態顯示另一個視圖控制器( vc2 )。 劇情的presentation style被設置為over current context

我還以編程方式在UIVisualEffectView上創建了UIVisualEffectView (它涵蓋了整個vc1view )。

現在,我需要一個視覺效果的效果設置為UIBlurEffect ,但之后vc2有模式顯示(其中viewbackgroundColour設為clear ,因此vc1仍落后可見vc2 )。

是否有可能無需編寫自定義視圖控制器轉換就可以實現?如果是,該怎么做? 如果您知道該怎么做,我們將不勝感激。

在iOS中,默認情況下不會進行模糊過渡。 因此,您需要自定義代碼。
一個簡單的解決方案是添加一個子視圖:

過渡到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)  

消除模糊視圖的功能(在vc1中):

func removeBlurView()
{
    NotificationCenter.default.removeObserver(self)
    blurEffectView.removeFromSuperview()
}  

關閉vc2時,在vc2中添加以下內容:

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "removeBlurView"), object: nil)

Ray的教程和一個緊密相關的教程所述,還有其他方法可以實現此目的

您可以使用子級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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM