SWRevealViewController 菜单打开时模糊 FontView

Posted

技术标签:

【中文标题】SWRevealViewController 菜单打开时模糊 FontView【英文标题】:Blur the FontView when SWRevealViewController menu opens 【发布时间】:2016-08-07 14:28:51 【问题描述】:

我在使用SWRevealViewController 创建的应用程序中使用幻灯片菜单。现在我想在用户打开菜单时模糊FrontView。这怎么可能?我的想法是在FrontViewController 中有一个alpha 值为0 的blurView。当用户打开菜单时,alpha 值变为 1。我的问题是当用户打开菜单时是否有一个特定的函数被调用?

如果可能,blurView's alpha 值应该与打开菜单的滑动一样变化:当用户使用GestureRecognizer 打开菜单时,menuViewController 的移动和 的变化alpha 值应该相等。

我希望你能理解我的问题。 感谢您的帮助!

【问题讨论】:

【参考方案1】:

我一直在研究你的问题,这是我的结果,

首先,在 SWRevealViewControllerDelegate 中,您有 3 种方法可以注意到revealViewController panGestureRecognizer 上的变化

func revealController(revealController: SWRevealViewController!, panGestureBeganFromLocation location: CGFloat, progress: CGFloat) 


func revealController(revealController: SWRevealViewController!, panGestureMovedToLocation location: CGFloat, progress: CGFloat, overProgress: CGFloat) 


func revealController(revealController: SWRevealViewController!, panGestureEndedToLocation location: CGFloat, progress: CGFloat, overProgress: CGFloat) 

所以你需要让你的FrontViewController 成为SWRevealViewController 的代表

喜欢这个self.revealViewController().delegate = self

然后使用基于教程提供的示例代码的https://www.raywenderlich.com/84043/ios-8-visual-effects-tutorial 的扩展名为UIImage+ImageEffects,我们可以从frontViewController.view 创建一个图像并应用上述类别中的高斯模糊这是代码

import UIKit

class MainViewController: UIViewController,SWRevealViewControllerDelegate 

    var imageBlur : UIImageView?
    var originalImage : UIImage?
    let maxRadius : CGFloat = 5.0
    let minRadius = 0
    var arrayOfImages : [UIImage]?

    override func viewDidLoad() 
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        self.revealViewController().delegate = self

    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    

    override func viewDidAppear(animated: Bool) 
        super.viewDidAppear(animated)
        self.originalImage = self.takeSnapShotOfView(self.view)
        self.imageBlur = UIImageView(image: self.originalImage)
        self.view.addSubview(self.imageBlur!)
    

    func revealController(revealController: SWRevealViewController!, panGestureBeganFromLocation location: CGFloat, progress: CGFloat) 

        let image = self.originalImage
        self.imageBlur?.image = image?.applyFixedBlurWithRadius(CGFloat(progress*self.maxRadius))
    

    func revealController(revealController: SWRevealViewController!, panGestureMovedToLocation location: CGFloat, progress: CGFloat, overProgress: CGFloat) 
        let image = self.originalImage
        self.imageBlur?.image = image?.applyFixedBlurWithRadius(CGFloat(progress*self.maxRadius))

    

    func revealController(revealController: SWRevealViewController!, panGestureEndedToLocation location: CGFloat, progress: CGFloat, overProgress: CGFloat) 
        let image = self.originalImage
        self.imageBlur?.image = image?.applyFixedBlurWithRadius(CGFloat(progress*self.maxRadius))

    

    func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition) 
        if(position == .Left)
        
            self.imageBlur?.image = self.originalImage
        
        if(position == .Right)
        
            let image = self.originalImage
            self.imageBlur?.image = image?.applyFixedBlurWithRadius(CGFloat(1*self.maxRadius))
        
    

    func takeSnapShotOfView(view:UIView) ->UIImage
    
        UIGraphicsBeginImageContext(CGSizeMake(view.frame.size.width, view.frame.size.height))
        view.drawViewHierarchyInRect(CGRectMake(0, 0, view.frame.size.width, view.frame.size.height), afterScreenUpdates: true)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return image
    



希望对你有帮助

【讨论】:

太棒了!非常感谢

以上是关于SWRevealViewController 菜单打开时模糊 FontView的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 SWRevealViewController 禁用侧边栏菜单中的交互

swrevealviewcontroller 在侧边菜单打开/关闭时更改切换按钮图像?

在 Swift 项目中实现侧滑菜单-利用 SWRevealViewController

self.revealViewController 变为 nil 并且 SWRevealViewController 菜单在从某个 VC 转回 SWRevelViewController 后不显示

如何为多个视图控制器使用 SWRevealviewcontroller 库

iOS:SWRevealViewController 打开没有导航栏的 WebView 控件