滑入iOS菜单

时间:2017-12-28 18:44:38

标签: ios swift cocoa-touch uiview

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

例如继承人截图(可能是我做过的事情): 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)
    }
}

1 个答案:

答案 0 :(得分:1)

永远记住0和0对于x和y总是在屏幕的左上角,如果宽度和高度是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)