简体   繁体   English

同时使用平移和滑动手势识别器

[英]Using Pan and Swipe gesture recognisers simultaneously

I've got a view on the bottom of my ViewController ( similar to Google Maps Bottom Sheet ). 我在ViewController的底部有一个视图(类似于Google Maps Bottom Sheet)。 The goal is: 目标是:

  1. When panning up, the view moves towards the pan direction ( essentially follows the finger), when panning ends, the view goes fullscreen. 平移时,视图朝平移方向移动(基本上跟随手指),平移结束时,视图变为全屏显示。 So far so good, all works. 到目前为止一切顺利。

  2. Adding swipe gestures. 添加滑动手势。 When swiping up, the view should go full screen. 向上滑动时,视图应全屏显示。

The issue is that by definition, a swiping gesture is a pan gesture but not the other way around. 问题是,按照定义,滑动手势是平移手势,而不是相反。 So unless i go really slow with my panning , the swipe gesture will trigger and the view will go full screen even though im still dragging on the screen. 因此,除非我平移的速度非常慢,否则即使我仍在屏幕上拖动,滑动手势也会触发并且视图将变为全屏。

Simply panning up doesnt look much like the kind of swipe im looking for. 只是平移看起来并不像我正在寻找的那种滑动。 The swipe gesture im describing should only trigger if the user "flicked" the view momentarily. im描述的滑动手势仅应在用户暂时“轻拂”视图时触发。 If they keep on panning the pan gesture should take precedence. 如果他们继续平移,则平移手势应优先。

Any ideas how to achieve this? 任何想法如何实现这一目标? For reference you could check tap on a pin on google maps on either android or ios. 作为参考,您可以在android或ios上检查一下Google地图上的图钉。

Its a little hard to describe without showing so if it helps im very open to clarify things. 如果不显示它很难描述,所以它是否有助于我非常开放地澄清事情。

UPDATES 更新

  1. I think the distinction for a swipe that would override the pan as im describing is that it a) took a short amount of time to complete b)the gesture ended with the user lifting the finger off the screen c) (maybe wrong ) the area traversed should not be too big. 我认为,按我所描述的那样会覆盖平移的滑动的区别是:a)花费了很短的时间才能完成b)手势以用户将手指从屏幕上移开而结束c)(可能错了)遍历不应太大。 Sounds a lot like a flick to me.. 对我来说听起来很像轻弹。

If you cannot describe the difference between the two gestures in clearly defined terms then it will be difficult to tell the UIGestureRecognizer how to do so. 如果您无法用明确定义的术语来描述两个手势之间的差异,那么将很难告诉UIGestureRecognizer如何做到。 In addition, it is possible that your user will have difficulty figuring out how to properly interact with the screen if your gestures are too similar or complex. 另外,如果您的手势过于相似或过于复杂,您的用户可能会难以确定如何与屏幕进行正确交互。

That being said, you may be able to distinguish between a "swipe" and a "pan" by checking the gesture's velocity . 话虽如此,您可以通过检查手势的速度来区分“滑动”和“摇动”。 You should be able to play around with the UIGestureRecognizerDelegate methods and achieve the effect you want. 您应该可以使用UIGestureRecognizerDelegate方法并达到所需的效果。

Ok i've achieved what was needed. 好的,我已经实现了所需要的。 The short answer is that i was looking for a "flick" gesture instead of a pan gesture. 简短的答案是我正在寻找“轻拂”手势而不是平移手势。 Generally its a nono to add custom gestures but as im replicating the google maps bottom sheet component for ios, it would appear that a custom gesture is the only way. 通常,它是添加自定义手势的一种方法,但是当我为ios复制google maps底部表单组件时,自定义手势似乎是唯一的方法。

Using Navillus's comment, I added a velocity and gesture end check in the pan recogniser. 使用Navillus的注释,我在平移识别器中添加了速度和手势结束检查。 The result looked like this: 结果看起来像这样:

if(recognizer.state == .ended){
        if(recognizer.velocity(in: self).y > CGFloat(500) ){
            self.pullUpViewSetMode_SUMMARY()
            return;
        }

        if(recognizer.velocity(in: self).y < CGFloat(-500) ){
            self.pullUpViewSetMode_FULL()
            return;
        }
}
//the rest of the pan handling code, namely translating the view up and up follows here

Hope this helps somebody. 希望这对某人有帮助。

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

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