Swift开发iOS学习专题@IT·互联网

Swift-QQ最新版抽屉侧滑和弹框视图

2017-03-13  本文已影响1113人  coderLiu

导读

  • 简单用Swift写了一个抽屉效果,可以直接使用并且简单;
  • 很多软件都运了抽屉效果,比如qq的左抽屉,英雄联盟,滴滴打车,和uber等等都运用了抽屉;
  • GitHub上有代表性的是MMDrawerController.

效果

image.gif

iOS抽屉式结构实现分析

        view.addSubview(leftMenuVC.view)
        view.addSubview(mainVC.view)
        
        addChildViewController(leftMenuVC)
        addChildViewController(mainVC)
//MARK: - 打开左侧菜单
    func openLeftMenu() {
        
        UIView.animate(withDuration: 0.25, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {
        
            self.leftVC?.view.transform = CGAffineTransform.identity
            self.mainVC?.view.transform = CGAffineTransform(translationX: self.maxWidth, y: 0)
            
        
        }, completion: {
        
            (finish: Bool) -> () in
            
            self.mainVC?.view.addSubview(self.coverBtn)
            
        })
        
    }
    
    //MARK: - 关闭左侧菜单
    func closeLeftMenu() {
        
        UIView.animate(withDuration: 0.25, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {
            
            self.leftVC?.view.transform = CGAffineTransform(translationX: -self.maxWidth, y: 0)
            self.mainVC?.view.transform = CGAffineTransform.identity
            
            
        }, completion: {
            
            (finish: Bool) -> () in
            
            self.coverBtn.removeFromSuperview()
            
        })
        
        
    }
for childViewController in (mainVC?.childViewControllers)! {
            
            addScreenEdgePanGestureRecognizerToView(view: childViewController.view)
            
        }
//MARK: - 添加屏幕边缘手势
    private func addScreenEdgePanGestureRecognizerToView(view: UIView) {
        
        let pan = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(edgPanGesture(_:)))
        pan.edges = UIRectEdge.left
        view.addGestureRecognizer(pan)
        
    }

//MARK: - 屏幕左边缘手势
    func edgPanGesture(_ pan: UIScreenEdgePanGestureRecognizer) {
        
        let offsetX = pan.translation(in: pan.view).x
        
        if pan.state == UIGestureRecognizerState.changed && offsetX <= maxWidth {
            
            mainVC?.view.transform = CGAffineTransform(translationX: max(offsetX, 0), y: 0)
            leftVC?.view.transform = CGAffineTransform(translationX: -maxWidth + offsetX, y: 0)
            
        } else if pan.state == UIGestureRecognizerState.ended || pan.state == UIGestureRecognizerState.cancelled || pan.state == UIGestureRecognizerState.failed {
            
            if offsetX > screenW * 0.5 {
                
                openLeftMenu()
                
            } else {
                
                closeLeftMenu()
            }
            
        }
        
    }

//MARK: - 遮盖按钮手势
    func panCloseLeftMenu(_ pan: UIPanGestureRecognizer) {
        
        let offsetX = pan.translation(in: pan.view).x
        if offsetX > 0 {return}
        
        if pan.state == UIGestureRecognizerState.changed && offsetX >= -maxWidth {
            
            let distace = maxWidth + offsetX
            
            mainVC?.view.transform = CGAffineTransform(translationX: distace, y: 0)
            leftVC?.view.transform = CGAffineTransform(translationX: offsetX, y: 0)
            
        } else if pan.state == UIGestureRecognizerState.ended || pan.state == UIGestureRecognizerState.cancelled || pan.state == UIGestureRecognizerState.failed {
            
            if offsetX > screenW * 0.5 {
                
                openLeftMenu()
                
            } else {
                
                closeLeftMenu()
            }
            
        }
        
    }

UIPresentationController简介

直接贴代码不讲废话

@IBAction func showAlert(_ sender: UIBarButtonItem) {
        
        let popVC = PopViewController()
        popVC.modalPresentationStyle = UIModalPresentationStyle.popover
        popVC.popoverPresentationController?.barButtonItem = sender
        popVC.popoverPresentationController?.delegate = self
        //可控制三角颜色
        popVC.popoverPresentationController?.backgroundColor = UIColor.white
        present(popVC, animated: true, completion: nil)
        
    }


    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.none
    }
    
    func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
        return true
    }

具体代码可以从这里下载,后续还有更实用的功能.

上一篇下一篇

猜你喜欢

热点阅读