【问题标题】:how to move the secondViewController from the top of the firstViewController using panGesture?如何使用 panGesture 从 firstViewController 的顶部移动 secondViewController?
【发布时间】:2018-09-03 17:18:36
【问题描述】:

我正在尝试使用 panGesture 将 secondViewController 移出 firstViewController 的顶部,但我遇到了问题...

当我开始移动 secondViewController 时,控制器后面会出现黑屏,而不是 firstViewController(正如我所料),我不知道如何解决这个问题......

这是说明我的问题的代码...

这里是第一个VC..

import UIKit

class mainController: UIViewController {

    let tapGestureRecognizer = UITapGestureRecognizer()
    let secondVC = SecondViewController()

    override func viewDidLoad() {
        super.viewDidLoad()

        tapGestureRecognizer.addTarget(self, action: #selector(goToSecondVC))

        view.backgroundColor = UIColor.red
        view.addGestureRecognizer(tapGestureRecognizer)
    }

    @objc func goToSecondVC(){
        present(secondVC, animated: true, completion: nil)
    }        
}

这里还有 secondVC..

import UIKit

class SecondViewController: UIViewController {

    let panGesture = UIPanGestureRecognizer()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.green

        panGesture.addTarget(self, action: #selector(moveView))

        view.addGestureRecognizer(panGesture)
    }

    @objc func moveView(pan: UIPanGestureRecognizer){
        let translation = pan.translation(in: view)

        if pan.state == .began{
            print("began")

        } else if pan.state == .changed {

            view.frame.origin.x += translation.x

            pan.setTranslation(CGPoint.zero, in: view)
        } else if pan.state == .ended{

            print("ended")                
        }
    }
}

为什么在第二个视图控制器后第二个视图控制器后面会出现黑屏?

【问题讨论】:

    标签: ios swift uiviewcontroller uipangesturerecognizer


    【解决方案1】:

    您只需要正确设置您的SecondViewControllermodalPresentationStyle...

    @objc func goToSecondVC(){
        secondVC.modalPresentationStyle = .overCurrentContext
        present(secondVC, animated: true, completion: nil)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-18
      • 1970-01-01
      相关资源
      最近更新 更多