gpt4 book ai didi

ios - 如何使用 panGesture 从 firstViewController 的顶部移动 secondViewController?

转载 作者:行者123 更新时间:2023-11-28 05:53:20 26 4
gpt4 key购买 nike

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

当我开始移动 secondViewController 时,黑屏出现在 Controller 后面,而不是 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")
}
}
}

为什么在第二个 View Controller 后面出现第二个 View Controller 后出现黑屏?

最佳答案

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

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

关于ios - 如何使用 panGesture 从 firstViewController 的顶部移动 secondViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52154031/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com