简体   繁体   English

如何检测滑动,但不能从边缘滑动?

[英]How can I detect a swipe, but not a swipe from the edge?

In my app, I want to detect an upwards swipe. 在我的应用程序中,我想检测向上滑动。 So of course, I added a UISwipeGestureRecognizer to the view of the view controller. 当然,我在视图控制器的view中添加了一个UISwipeGestureRecognizer

As expected, this recognizer detects swipe very well. 正如所料,这个识别器可以很好地检测滑动。 But a bad thing about this is that a swipe from bottom edge is also detected. 但关于这一点的一个坏处是也检测到从底部边缘的滑动。 When the user wants to open the control center: 当用户想要打开控制中心时:

在此输入图像描述

a swipe is also detected. 还检测到滑动。 I don't want this to happen. 我不希望这种情况发生。

In other words, I want to detect all upwards swipes except those from the bottom edge of the screen . 换句话说, 我想检测除了屏幕下边缘以外的所有向上滑动

I think I can use a UIEdgePanGestureRecognizer to detect an edge swipe as well. 我想我也可以使用UIEdgePanGestureRecognizer来检测边缘滑动。 And when such a swipe is detected, disable the swipe recognizer. 并且当检测到这种滑动时,禁用滑动识别器。 But this is barely possible right? 但这几乎不可能吗? Because who knows which recognizer detects a gesture first? 因为谁知道哪个识别器首先检测到手势?

How can I do this? 我怎样才能做到这一点?

PS I think I need to use requireGestureRecognizerToFail , but the documentation is so bad I can't fully understand. PS我想我需要使用requireGestureRecognizerToFail ,但文档太差了我无法完全理解。

You can try hiding status bar so there wont be accidental control center openings. 您可以尝试隐藏状态栏,这样就不会出现意外的控制中心开口。 With this when user swipe from bottom, an arrow will appear. 当用户从底部滑动时,会出现一个箭头。 If still user wants to open control center, he/she needs to swipe again. 如果仍然用户想要打开控制中心,他/她需要再次滑动。

Try code below 请尝试以下代码

override func prefersStatusBarHidden() -> Bool {
return true
}

There is one more solution for this, but I am not sure if Apple will be angry of this. 还有一个解决方案,但我不确定Apple是否会对此感到生气。

In .plist there is value named "View controller-based status bar appearance". 在.plist中,有一个名为“View controller-based status bar appearance”的值。 Set it to NO 将其设置为NO

In this case you should use this delegate method 在这种情况下,您应该使用此委托方法

optional func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool

Check otherGestureRecognizer and return NO if you don't want your recogniser to fire simultaneously with it. 如果您不希望识别器同时otherGestureRecognizer ,请检查otherGestureRecognizer并返回NO

Try this and see whether the system calls this method with the gesture recogniser you're trying to avoid. 试试这个,看看系统是否用你想要避免的手势识别器调用这个方法。 If not, consider adding your own UIScreenEdgePanGestureRecognizer with UIRectEdge.Bottom to check against that instead. 如果没有,请考虑使用UIRectEdge.Bottom添加您自己的UIScreenEdgePanGestureRecognizer来检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM