iOS 的滑入菜单

Slide in Menu for iOS

我正在尝试在菜单中创建一个从左侧进入的幻灯片,但我做了一些事情,它却进入了右侧。我已经以编程方式完成了这个未使用的情节提要,因为大多数时候约束都搞砸了。

例如,这是一张截图(可能是我做过的): Slide Menu - Gone Wrong

import UIKit

class SideInMenu: NSObject {

    let blackView = UIView()

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

    @objc func showSlideMenu() {
        //Show slide in menu Here

        if let window = UIApplication.shared.keyWindow {
            blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)

            blackView.addGestureRecognizer(UITapGestureRecognizer(target:
                self, action: #selector(handleDismiss)))

            window.addSubview(blackView)
            window.addSubview(collectionView)

            let width: CGFloat = 194
            let x = window.frame.width - width
            collectionView.frame = CGRectMake(x, 0, width, window.frame.height)
            //collectionView.frame = CGRectMake(x, 0, width, window.frame.height)

            blackView.frame = window.frame
            blackView.alpha = 0

            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping:
                1, initialSpringVelocity: 1, options: .curveEaseOut,
                animations: {
                self.blackView.alpha = 1

                self.collectionView.frame = self.CGRectMake(width, 0, self.collectionView.frame.width, self.collectionView.frame.height)

            }, completion: nil)

        }
    }

    @objc func handleDismiss() {
        UIView.animate(withDuration: 0.5) {
            self.blackView.alpha = 0
            if let window = UIApplication.shared.keyWindow {
                self.collectionView.frame = self.CGRectMake(window.frame.width, 0, self.collectionView.frame.width, self.collectionView.frame.height)
            }
        }

    }

    override init() {
        super.init()
    }

    func CGRectMake(_ x: CGFloat, _ y: CGFloat, _ width: CGFloat, _ height: CGFloat) -> CGRect {return CGRect(x: x, y: y, width: width, height: height)
    }
}

永远记住,x 和 y 的 0, 0 始终是屏幕的左上角,如果宽度和高度是 20, 20,它将从左上角向右延伸 20px,向下延伸 20px。在 viewDidLoad 内部要做的第一件事是设置 collectionViews 初始框架,它像这样在屏幕外。 别忘了负宽度:

self.collectionView.frame = self.CGRectMake(-width, 0, self.collectionView.frame.width, self.collectionView.frame.height)

像这样从左边开始动画:

        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping:
            1, initialSpringVelocity: 1, options: .curveEaseOut,
            animations: {
            self.blackView.alpha = 1

            self.collectionView.frame = self.CGRectMake(0, 0, self.collectionView.frame.width, self.collectionView.frame.height)

        }, completion: nil)

解雇:

        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping:
            1, initialSpringVelocity: 1, options: .curveEaseOut,
            animations: {
            self.blackView.alpha = 1

            self.collectionView.frame = self.CGRectMake(-width, 0, self.collectionView.frame.width, self.collectionView.frame.height)

        }, completion: nil)