0

I use a slide menu in my app which i created with SWRevealViewController. Now I want to blur the FrontView when the user opens the menu. How is that possible? My idea would be to have a blurView in the FrontViewController with an alpha value of 0. When the user opens the menu the alpha value changes to 1. My question is if there is a specific function which is called when the user opens the menu?

If possible the blurView's alpha value should change equally to the swipe which opens the menu: When the user uses the GestureRecognizer to open the menu, the movement of the menuViewController and the change of the alpha value should go equal.

I hope you can understand my problem. Thank You for your help!

Asdrubal
  • 2,421
  • 4
  • 29
  • 37

1 Answers1

0

I had been working on your question and this are my results,

enter image description here

First of all in the SWRevealViewControllerDelegate you have 3 methods to get noticed of changes on the 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) {
}

So you need make your FrontViewController as delegate of SWRevealViewController

like this self.revealViewController().delegate = self

and then using an extension from https://www.raywenderlich.com/84043/ios-8-visual-effects-tutorial called UIImage+ImageEffects founded on the example code provided by the tutorial, we can create an image from frontViewController.view and apply the gaussian blur from the mentioned category this is the code

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
    }


}

I hope this helps you

Reinier Melian
  • 20,519
  • 3
  • 38
  • 55