【问题标题】:Horizontal Slide-in Menu水平滑入式菜单
【发布时间】:2018-02-12 13:27:07
【问题描述】:

我是 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)
    }
}

【问题讨论】:

    标签: ios swift4 xcode9.2


    【解决方案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)
    

    【讨论】:

    • 你能提供解决办法吗?
    • answer contains fix change x here collectionView.frame = CGRect(x: -1 * collectionWidth , y: 0, width: collectionWidth, height: window.frame.height) 在 UIView.animate 之前的行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多