Class to add Slider Menu - swift 4

0

I want to create a class that I can invoke at any time in my program to be able to bring a slider menu to screen.

Create a class called MenuV that implements NSObject. This class creates a view with a blue alpha and at the same time shows a collection view in a part of the screen.

import UIKit

class MenuV: NSObject{

let backView = UIView()
let collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.backgroundColor = .white
    return cv
}()

func showMenu(){
    if let window = UIApplication.shared.keyWindow {
        backView.backgroundColor = UIColor.blue.withAlphaComponent(0.7)
        backView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss)))

        window.addSubview(backView)
        window.addSubview(collectionView)

        collectionView.frame = CGRect(x: window.frame.maxX * -1, y: 0, width: window.frame.width * 0.75, height: window.frame.height)

        backView.frame = window.frame
        backView.alpha = 0

        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
            self.backView.alpha = 1
            self.collectionView.frame = CGRect(x: 0, y: 0, width: self.collectionView.frame.width, height: self.collectionView.frame.height)
        }, completion: nil)
    }
}

@objc func handleDismiss() {
    UIView.animate(withDuration: 0.5) {
        self.backView.alpha = 0
        if let window = UIApplication.shared.keyWindow {
            self.collectionView.frame = CGRect(x: window.frame.maxX * -1, y: 0, width: window.frame.width * 0.5, height: window.frame.height)
        }
    }
}

override init(){
    super.init()
}

}

Everything is great, when I create an instance and invoke the showMenu method, a view of the size of the entire semi-transparent screen is added and a white-background collection view comes out of the right.

 let menuV = MenuV()
 menuV.showMenu()

The idea is that once you tap on the backView, the handleDismiss method is executed, which must disappear.

As you can see in the code, I am using UIApplication.shared.keyWindow to be able to reference the screen of the device in the use of coordinates.

However, I am realizing that once you add the views on the window, they go to the bottom and the gesture is not recognized.

First of all, it occurs to me that I have to find a way to send these views to the top of the screen so that they begin to respond to the gestures.

I am based on the video How to Create a Slide-In Menu Part 1 , apparently he is using switf 3, but I do not see what I'm doing wrong.

    
asked by Salvador Nava 15.07.2018 в 04:32
source

1 answer

1

I was reviewing your question and I found an answer that they published, I tried it and it works, I attach the answer and the link.

import UIKit

class ViewController: UIViewController {
lazy var menuV: MenuV = {
    let menuController = MenuV()
    return menuController
}()


override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction func cambioEstadoBoton(boton: UIButton) {
    print("Entra a cambio de estado")
    menuV.showMenu()
}
}

Stackoverflow answer attached

Greetings

    
answered by 19.07.2018 / 00:18
source