水平滑入菜单

时间:2018-02-12 13:27:08

标签: ios swift4 xcode9.2

我对iOS / Swift开发很陌生,我尝试实现水平滑入式菜单(用户设置)。

我设法让它发挥作用,我面临的问题是,菜单从右向左滑动,但它应该反过来,即从左到右。我试图使用正值和负值,但我仍然对这些CG属性如何工作(轴x,y等)感到有些困惑。

这是我的代码,负责显示菜单:

     private func showSettingsMenu(){
        if let window = UIApplication.shared.keyWindow {

        blackView.backgroundColor = UIColor(white: 0, alpha: 0.7)

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

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

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

        let collectionWidth: CGFloat = window.frame.width * 0.75
        let x = window.frame.width - collectionWidth

        collectionView.frame = CGRect(x: window.frame.width, y: 0, width: collectionWidth, height: window.frame.height)

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

            self.blackView.alpha = 1
            self.collectionView.frame = CGRect(x: x, y: 0, width: self.collectionView.frame.width, height: self.collectionView.frame.height)

        }, completion: nil)
    }
}

1 个答案:

答案 0 :(得分:1)

问题在于您将初始x位置设置为屏幕宽度,将最终位置设置为1/4,以便从右向左滑动:

let collectionWidth: CGFloat = window.frame.width * 0.75

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

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

   self.blackView.alpha = 1

   self.collectionView.frame = CGRect(x: 0, y: 0, width: self.collectionView.frame.width, height: self.collectionView.frame.height)

}, completion: nil)

修改更改行

 collectionView.frame = CGRect(x: window.frame.width, y: 0, width: collectionWidth, height: window.frame.height)

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

///在动画更改行内

 self.collectionView.frame = CGRect(x: x, y: 0, width: self.collectionView.frame.width, height: self.collectionView.frame.height)

 self.collectionView.frame = CGRect(x: 0, y: 0, width: self.collectionView.frame.width, height: self.collectionView.frame.height)