控制中心视图

时间:2018-02-12 06:25:57

标签: ios swift

我试图在iPhone中创建一个像控制中心这样的视图。根据我的要求,当用户交换时,视图必须位于底部,View需要从下到上存在,而解雇视图需要交换底部。正如控制中心一样,我需要一个视图,任何人都可以帮助我吗? / p>

1 个答案:

答案 0 :(得分:1)

您可以通过以下代码实现:

class ViewController: UIViewController, UIGestureRecognizerDelegate {

    var viewControlCenter : UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.
        createControlCenterView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // MARK: - Gesture Methods

    @objc func respondToSwipeGesture(sender: UIGestureRecognizer? = nil) {
        if let swipeGesture = sender as? UISwipeGestureRecognizer {
            switch swipeGesture.direction {
            case UISwipeGestureRecognizerDirection.up:
                if self.viewControlCenter.frame.origin.y != 0 {
                    UIView.animate(withDuration: 0.5, animations: {
                        self.viewControlCenter.frame.origin.y = 0
                    }, completion: nil)
                }
                break
            case UISwipeGestureRecognizerDirection.down:
                if self.viewControlCenter.frame.origin.y == 0 {
                    UIView.animate(withDuration: 0.5, animations: {
                        self.viewControlCenter.frame.origin.y = self.view.frame.size.height
                    }, completion: nil)
                }
                break
            default:
                break
            }
        }
    }

    @objc func onViewTapped(sender: UITapGestureRecognizer? = nil) {
        if self.viewControlCenter.frame.origin.y == 0 {
            UIView.animate(withDuration: 0.5, animations: {
                self.viewControlCenter.frame.origin.y = self.view.frame.size.height
            }, completion: nil)
        }
    }

    // MARK: - Custom Methods

    func createControlCenterView() {
        viewControlCenter = UIView.init(frame: self.view.bounds)
        self.viewControlCenter.frame.origin.y = self.view.frame.size.height

        let viewBG : UIView = UIView.init(frame: self.view.bounds)
        viewBG.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.6)
        viewControlCenter.addSubview(viewBG)

        self.view.addSubview(viewControlCenter)

        let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(sender:)))
        swipeUp.direction = .up
        self.view.addGestureRecognizer(swipeUp)

        let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(sender:)))
        swipeDown.direction = .down
        self.view.addGestureRecognizer(swipeDown)

        let tap = UITapGestureRecognizer(target: self, action: #selector(onViewTapped(sender:)))
        tap.delegate = self
        viewControlCenter.addGestureRecognizer(tap)
    }
}