如何在Swift 5的菜单中创建幻灯片

时间:2020-04-22 09:49:09

标签: ios swift animation menu

我看过很多教程,如何在菜单中创建幻灯片,但是它们不起作用。这些教程太旧了。

有人可以告诉我如何创建一个从视图底部滑入的菜单吗?在下面的链接中,您可以看到示例图像。

Slide in menu from bottom

这是我尝试执行的一些代码:

var showAlertViewBtn: UIButton = {
   let button = UIButton()
    button.setTitle("Show", for: .normal)
    button.backgroundColor = .blue
    button.translatesAutoresizingMaskIntoConstraints = false
    button.addTarget(self, action: #selector(showAlertView), for: .touchUpOutside)
    return button
}()

var customAlertView: UIView = {
    let view = UIView()
    view.backgroundColor = .white
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()


var customAlertTextLabel: UILabel = {
       let label = UILabel()
       label.font = UIFont(name: "Ubuntu-Bold", size: 16)
       label.text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur"
       label.textColor = .black
    label.numberOfLines = 0
       label.translatesAutoresizingMaskIntoConstraints = false
       return label
   }()


var customAlertBtn: UIButton = {
   let button = UIButton()
    button.setTitle("Cancel", for: .normal)
    button.backgroundColor = .blue
    button.translatesAutoresizingMaskIntoConstraints = false
    button.addTarget(self, action: #selector(customAlertBtnTapped), for: .touchUpOutside)
    return button
}()

    var customAlertViewBottomConstraint: NSLayoutConstraint!

@objc func customAlertBtnTapped() {
      print("customAlertBtnTapped tapped")

      self.customAlertViewBottomConstraint.constant = 400
  }

@objc func showAlertView() {
    UIView.animate(withDuration: 2.0) {
        self.customAlertViewBottomConstraint.constant = 0
    }
}

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


func setUpAlertView() {

    view.addSubview(showAlertViewBtn)
    showAlertViewBtn.heightAnchor.constraint(equalToConstant: 50).isActive = true
    showAlertViewBtn.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    showAlertViewBtn.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    showAlertViewBtn.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true



       view.addSubview(customAlertView)
       //customAlertView.frame = CGRect(x: 0, y: UIScreen.main.bounds.height, width: UIScreen.main.bounds.width, height: 400)
       customAlertView.heightAnchor.constraint(equalToConstant: 400).isActive = true
       customAlertView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
       customAlertView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
       customAlertViewBottomConstraint = view.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 400)
       customAlertViewBottomConstraint.isActive = true

       customAlertView.addSubview(customAlertTextLabel)
       customAlertView.addSubview(customAlertBtn)
       customAlertTextLabel.leftAnchor.constraint(equalTo: customAlertView.leftAnchor).isActive = true
       customAlertTextLabel.rightAnchor.constraint(equalTo: customAlertView.rightAnchor).isActive = true
       customAlertTextLabel.bottomAnchor.constraint(equalTo: customAlertBtn.topAnchor, constant: -20).isActive = true


       customAlertBtn.leftAnchor.constraint(equalTo: customAlertView.leftAnchor).isActive = true
       customAlertBtn.rightAnchor.constraint(equalTo: customAlertView.rightAnchor).isActive = true
       customAlertBtn.heightAnchor.constraint(equalToConstant: 50).isActive = true
       customAlertBtn.bottomAnchor.constraint(equalTo: customAlertView.bottomAnchor, constant:  -40).isActive = true

   }

1 个答案:

答案 0 :(得分:0)

那是我在“菜单中的幻灯片”中使用的那段代码的片段。

    func animateMenu(shouldExpand: Bool, completion: ((Bool) -> Void)? = nil) {
        if shouldExpand {
            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
                self.homeController.view.frame.origin.x = self.xOrigin
                self.blackView.alpha = 1
            }, completion: nil)
        } else {
            self.blackView.alpha = 0
            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
                self.homeController.view.frame.origin.x = 0
            }, completion: completion)
        }

        animateStatusBar()
    }

    func animateStatusBar() {
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
            self.setNeedsStatusBarAppearanceUpdate()
        }, completion: nil)
    }

希望它将对您有帮助