Как создать слайд в меню в Swift 5 - PullRequest
0 голосов
/ 22 апреля 2020

Я видел много уроков по созданию слайдов в меню, но они не работают. Уроки слишком старые.

Может кто-нибудь подсказать, как создать меню, которое выдвигается из нижней части экрана? В следующей ссылке вы можете увидеть пример изображения.

Слайд в меню снизу

Вот код, где я пытался это сделать:

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 голосов
/ 22 апреля 2020

Это код, который я использовал для моего слайда в меню.

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

Надеюсь, он вам поможет

...