我的应用中有一个UISplitViewController
,我只想从屏幕左边缘滑动即可打开主视图面板。
presentsWithGesture
添加swipe gesture
,但可以从局部视图的任何位置添加。因此,我将其设置为false并实现了自定义UIScreenEdgePanGestureRecognizer
,但是如果您连续滑动多次,它将崩溃。
这是我在appDelegate
中的工作。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let viewMaster = UIViewController()
let viewDetail = MyFirstViewController()
let navigationController = UINavigationController(rootViewController: viewDetail)
let splitViewController = UISplitViewController()
splitViewController.viewControllers = [viewMaster, navigationController]
splitViewController.view.backgroundColor = .clear
splitViewController.maximumPrimaryColumnWidth = 320
splitViewController.preferredDisplayMode = .primaryHidden
splitViewController.presentsWithGesture = false
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = splitViewController
self.window?.makeKeyAndVisible()
return true
}
这是我的FirstViewController中的
class MyFirstViewController: UIViewController {
override func viewDidLoad() {
self.view.backgroundColor = .red
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
swipeFromEdge()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func swipeFromEdge() {
let edgePan = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(screenEdgeSwiped))
edgePan.edges = .left
self.view.addGestureRecognizer(edgePan)
}
@objc func screenEdgeSwiped(_ recognizer: UIScreenEdgePanGestureRecognizer) {
if recognizer.state == .recognized {
if let splitView = self.splitViewController {
splitView.preferredDisplayMode = .primaryOverlay
}
}
}}
如果我连续多次快速滑动,应用程序将崩溃
2018-07-13 09:51:14.983707 + 0200 testGestureCrash [12126:1738716] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[UIPopoverController _presentPopoverBySlidingIn:fromEdge:ofView:animated:stateOnly :notifyDelegate:]:无法从没有窗口的视图中显示弹出窗口。 * 第一个调用堆栈: (0x18425ad8c 0x1834145ec 0x18425ac6c 0x18e8c442c 0x18e8c5868 0x18e8ca3b4 0x18e0466e8 0x18e5b33b4 0x18e1a8e38 0x18e045740 0x18e5a4bd4 0x18e03f4d8 0x18e03f010 0x18e03e874 0x18e03d1d0 0x18e81ed1c 0x18e8212c8 0x18e81a368 0x184203404 0x184202c2c 0x18420079c 0x184120da8 0x186105020 0x18e13d758 0x100fe0e6c 0x183bb1fc0) libc ++ abi.dylib:以类型为NSException的未捕获异常终止 (lldb)
您是否知道错误来自何处?
或者,也许您对我如何仅对UISplitViewController
进行边缘滑动有一个更好的主意?