首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >控制中心视图

控制中心视图
EN

Stack Overflow用户
提问于 2018-02-12 14:25:57
回答 1查看 76关注 0票数 0

我试图在iPhone中创建一个类似于控制中心的视图。根据我的要求,视图必须在底部当用户交换时,视图需要从下到上呈现,而解除视图需要交换bottom.Exactly喜欢控制中心我需要一个视图,有人能帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2018-02-12 17:48:00

您可以通过以下代码来实现这一点:

代码语言:javascript
复制
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)
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48740644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档