Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support dimming when partially revealed #385

Open
triztian opened this issue May 28, 2020 · 2 comments
Open

Support dimming when partially revealed #385

triztian opened this issue May 28, 2020 · 2 comments

Comments

@triztian
Copy link

triztian commented May 28, 2020

Is it possible to toggle the primaryContentViewController dimming when the drawer is partially revealed?, I know it gets dimmed when in .open. I'm looking to be able to do something like this:

let pulley = PulleyViewController()

pulley.setDrawerPosition(.partiallyRevealed)
pulley.dimPrimaryContent = true

Having a property that toggles dimming would be great, another potential solution would be for the PulleyDrawerViewControllerDelegate to have a openDrawerHeight(bottomSafeArea:) method that provides height for the .open position this way we could make it the desired size with dimming.

@iori57
Copy link

iori57 commented Dec 1, 2020

I'm looking for the exact same functionality, @triztian did you managed to work out something?

@triztian
Copy link
Author

triztian commented Dec 3, 2020

@iori57 Yes, I created a "Dimmable" protocol and extended PulleyController to check for that type; it is that type It'll inject a view to the primary controller. It's roughly like this:

protocol DimmableViewController: AnyObject {
    var isDimmed: Bool { get set }
    var dimAnimationDuration: TimeInterval { get set }
    var dimOpacity: CGFloat { get set }
    var dimColor: UIColor { get set }
}

Here's my base dimmable controller that I extend:

class BaseDimmableViewController: UIViewController, DimmableViewController {
    private(set) lazy var dimOverlayView: UIView = {
        let overlay = UIView(frame: view.frame)
        overlay.translatesAutoresizingMaskIntoConstraints = false
        overlay.backgroundColor = dimColor
        overlay.alpha = dimOpacity
        overlay.tag = 42
        return overlay
    }()

    private(set) lazy var dimNavbarOverlay: UIView? = {
        guard let navBarViewFrame = navigationController?.view.frame else { return nil }
        let overlay = UIView(frame: navBarViewFrame)
        overlay.translatesAutoresizingMaskIntoConstraints = false
        overlay.backgroundColor = dimColor
        overlay.alpha = dimOpacity
        overlay.tag = 43
        return overlay
    }()

    var isDimmed: Bool = false {
        didSet {
            if isViewLoaded && isDimmed {
                if dimAnimationDuration > 0.0 {
                    dimOverlayView.alpha = 0.0
                }

                if let navBarView = navigationController?.view,
                    let dimNavbarOverlay = dimNavbarOverlay {
                    if dimAnimationDuration > 0.0 {
                        dimNavbarOverlay.alpha = 0.0
                    }
                    navBarView.addSubview(dimNavbarOverlay)
                } else {
                    view.addSubview(dimOverlayView)
                }

                UIView.animate(withDuration: dimAnimationDuration) {
                    self.dimOverlayView.alpha = self.dimOpacity
                    self.dimNavbarOverlay?.alpha = self.dimOpacity
                }
            } else {
                if dimAnimationDuration <= 0.0 {
                    if let navBarView = navigationController?.view,
                        navBarView.subviews.last?.tag == 43 {
                        navBarView.subviews.last?.removeFromSuperview()
                    } else {
                        if view.subviews.last?.tag == 42 {
                            view.subviews.last?.removeFromSuperview()
                        }
                    }
                } else {
                    let contentOverlay = self.view.subviews.last
                    let navOverlay = self.navigationController?.view.subviews.last

                    UIView.animate(withDuration: dimAnimationDuration, animations: {
                        if contentOverlay?.tag == 42 {
                            contentOverlay?.alpha = 0.0
                        }

                        if navOverlay?.tag == 43 {
                            navOverlay?.alpha = 0.0
                        }
                    }, completion: { _ in
                        if contentOverlay?.tag == 42 {
                            contentOverlay?.removeFromSuperview()
                        }

                        if navOverlay?.tag == 43 {
                            navOverlay?.removeFromSuperview()
                        }
                    })
                }
            }
        }
    }

    var dimAnimationDuration: TimeInterval = 0.3
    var dimOpacity: CGFloat = 0.3

    var dimColor: UIColor = .black
}

And finally my custom PulleyController:

class DimmingPulleyViewController: PulleyViewController {
   var dimPartiallyRevealed = false 

   override func setDrawerPosition(position: PulleyPosition, 
                                                           animated: Bool, 
                                                           completion: PulleyAnimationCompletionBlock? = nil) {
      if dimPartiallyRevealed, let dimmableController = primaryContentViewController as? DimmableViewController {
         dimmableController.isDimmed = position == .partiallyRevealed
         // `.open` will be handled by the `PulleyViewController`
      } 

      super.setDrawerPosition(position: position, animated: animated, completion: completion)
   }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants