ios - how to move the secondViewController from the top of the firstViewController using panGesture?
问题描述
I'm trying to move the secondViewController off the firstViewController's top using panGesture but I'm running into a problem...
when I start moving the secondViewController a black screen show up right behind the controller and not the firstViewController (as I expected) and I don't know how to fix this problem...
here's the code that illustrate my problem...
firstVC here..
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)
}
}
and secondVC here..
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")
}
}
}
why there's a black screen behind the second view controller after present secondViewController?
解决方案
您只需要正确设置您SecondViewController
的 ' modalPresentationStyle
...</p>
@objc func goToSecondVC(){
secondVC.modalPresentationStyle = .overCurrentContext
present(secondVC, animated: true, completion: nil)
}
推荐阅读
- c# - 是否可以验证数字签名并创建新文档,以便 Adobe 阅读器或福昕阅读器在启动时不应该验证相同
- android - 有什么方法可以从 ref 获取数据作为 java 中的列表
- mysql - 如何使用 JPA 更新 LocalDateTime 字段?
- sprite - 在移相器中使用几乎相同的精灵的好习惯。使用更多精灵的替代方法
- arrays - React 将数据推送到数组对象中
- rubymotion - NDK 错误:Android 安装
- php - 如何将 HAM Maidenhead Grid Squares 转换为经纬度?
- cytoscape.js - 我正在使用 cytoscape 设置“text-opacity:0”的样式,但它不起作用
- android - 编译 Flutter 时出现“任务 ':app:packageDebug' 执行失败 > 无法生成 v1 签名”异常
- swift - 在 SwiftUI 中不断重复动作