美文网首页
swift addchildviewcontroller

swift addchildviewcontroller

作者: 松龄学编程 | 来源:发表于2020-06-07 09:41 被阅读0次
    func tapAtLabel() {
            guard let naviController = navigationController,
                let naviView = naviController.view else { return }
            let controller = FollowUserViewController()
            
            controller.view.translatesAutoresizingMaskIntoConstraints = false
            
            naviView.addSubview(controller.view)
            NSLayoutConstraint.activate([
                controller.view.leftAnchor.constraint(equalTo: naviView.leftAnchor),
                controller.view.rightAnchor.constraint(equalTo: naviView.rightAnchor),
                controller.view.topAnchor.constraint(equalTo: naviView.topAnchor),
                controller.view.bottomAnchor.constraint(equalTo: naviView.bottomAnchor)
            ])
            controller.view.setNeedsLayout()
            controller.view.layoutIfNeeded()
            
            naviController.addChild(controller)
            controller.didMove(toParent: naviController)
        }
    
    class FollowUserViewController: UIViewController {
        
        let followView = FollowUserView()
        
        var constraint: NSLayoutConstraint?
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            view.backgroundColor = UIColor.black.withAlphaComponent(0.4)
            
            let minHeight: CGFloat = 80
            var height: CGFloat = UIScreen.main.bounds.height * 0.7
            if FDDevice.hasTopNotch {
                height += 34
            }
            
            followView.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(followView)
            NSLayoutConstraint.activate([
                followView.leftAnchor.constraint(equalTo: view.leftAnchor),
                followView.rightAnchor.constraint(equalTo: view.rightAnchor),
                followView.heightAnchor.constraint(equalToConstant: height),
            ])
            
            constraint = followView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: height)
            constraint?.isActive = true
                    
            followView.offsetYCallBack = { [weak self] constant in
                guard let `self` = self else { return }
                if constant <= 0 {
                    self.constraint?.constant = 0
                } else if constant >= height - minHeight {
                    self.constraint?.constant = height - minHeight
                } else {
                    self.constraint?.constant = constant
                }
                
            }
            followView.panEndCallBack = { [weak self] offsetY in
                guard let `self` = self else { return }
                if offsetY <= height / 2 {
                    self.doShowUpAnimation()
                } else {
                    self.leave()
                }
            }
        }
        
        override func didMove(toParent parent: UIViewController?) {
            super.didMove(toParent: parent)
            doShowUpAnimation()
        }
        
        func doShowUpAnimation() {
            constraint?.constant = 0
            UIView.animate(withDuration: 0.15) {
                self.view.setNeedsLayout()
                self.view.layoutIfNeeded()
            }
        }
        
        func leave() {
            view.removeFromSuperview()
            willMove(toParent: nil)
            removeFromParent()
        }
    }
    

    相关文章

      网友评论

          本文标题:swift addchildviewcontroller

          本文链接:https://www.haomeiwen.com/subject/qhwxtktx.html