在SWRevealViewController菜单打开时模糊FontView

时间:2016-08-07 14:28:51

标签: ios swift swrevealviewcontroller

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

如果可能,blurView's alpha值应该同等地更改为打开菜单的滑动:当用户使用GestureRecognizer打开菜单时,menuViewController的移动和更改 alpha值应该相等。

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

1 个答案:

答案 0 :(得分:0)

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

enter image description here

首先在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

然后使用基于本教程提供的示例代码的名为UIImage+ImageEffects的{​​{3}}扩展,我们可以从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
    }


}

我希望这有助于你