如何使用 panGesture 从 firstViewController 的顶部移动 secondViewController?
how to move the secondViewController from the top of the firstViewController using panGesture?
我正在尝试使用 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)
}
}
这里是第二个VC..
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")
}
}
}
为什么出现secondViewController后第二个viewcontroller后面黑屏?
你只需要正确设置你的SecondViewController
的modalPresentationStyle
…
@objc func goToSecondVC(){
secondVC.modalPresentationStyle = .overCurrentContext
present(secondVC, animated: true, completion: nil)
}
我正在尝试使用 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)
}
}
这里是第二个VC..
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")
}
}
}
为什么出现secondViewController后第二个viewcontroller后面黑屏?
你只需要正确设置你的SecondViewController
的modalPresentationStyle
…
@objc func goToSecondVC(){
secondVC.modalPresentationStyle = .overCurrentContext
present(secondVC, animated: true, completion: nil)
}