UIPanGestureRecognizer - 仅垂直或水平
我有一个视图,它有一个 UIPanGestureRecognizer
来垂直拖动视图。所以在识别器回调中,我只更新 y 坐标来移动它。该视图的超级视图有一个 UIPanGestureRecognizer
,它将水平拖动视图,仅更新 x 坐标。
问题是第一个 UIPanGestureRecognizer
正在获取垂直移动视图的事件,因此我无法使用超级视图手势。
我已经尝试过了
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:
(UIGestureRecognizer *)otherGestureRecognizer;
,两者都会起作用,但我不想要这样。我希望仅当运动明显水平时才能检测到水平方向。因此,如果 UIPanGestureRecognizer
具有方向属性,那就太好了。
我怎样才能实现这种行为?我发现这些文档非常令人困惑,所以也许有人可以在这里更好地解释它。
I have a view that has a UIPanGestureRecognizer
to drag the view vertically. So in the recognizer callback, I only update the y-coordinate to move it. The superview of this view, has a UIPanGestureRecognizer
that will drag the view horizontally, just updating the x-coordinate.
The problem is that the first UIPanGestureRecognizer
is taking the event to move the view vertically, so I can not use the superview gesture.
I have tried
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:
(UIGestureRecognizer *)otherGestureRecognizer;
and both will work, but I don't want that. I want the horizontally to be detected only if the movement is clearly horizontal. So it would be great if the UIPanGestureRecognizer
had a direction property.
How can I achieve this behavior? I find the docs very confusing, so maybe someone can explain it better here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(22)
只需对垂直平移手势识别器执行此操作,它对我有用:
对于 Swift:
Just do this for the vertical pan gesture recognizer, it works for me:
And for Swift:
我创建了一个具有子类化的解决方案,如 @LocoMike 提供的答案中所示,但使用了 @Hejazi 提供的通过初始速度的更有效的检测机制。我也在使用 Swift,但如果需要的话,这应该很容易转换为 Obj-C。
相对于其他解决方案的优点:
这是代码:
使用示例:
I created a solution with subclassing like in the answer @LocoMike provided, but used the more effective detection mechanism via initial velocity as provided by @Hejazi. I'm also using Swift, but this should be easy to translate to Obj-C if desired.
Advantages over other solutions:
Here's the code:
Example of usage:
我想出了创建 UIPanGestureRecognizer
DirectionPanGestureRecognizer 的子类:
DirectionPanGestureRecognizer.m:
只有当用户开始拖动所选行为时,才会触发手势。将方向属性设置为正确的值即可。
I figured it out creating a subclass of UIPanGestureRecognizer
DirectionPanGestureRecognizer:
DirectionPanGestureRecognizer.m:
This will only trigger the gesture if the user starts dragging in the selected behavior. Set the direction property to a correct value and you are all set.
我尝试使用 UIPanGestureRecognizer 水平限制有效区域。
然后,只有在 thresholdAngle 度内水平滑动才能触发此平移手势。
I tried to constrain the valid area horizontally with UIPanGestureRecognizer.
Then, only swiping within thresholdAngle degree horizontally can trigger this pan gesture.
Swift 3.0 答案:仅处理垂直手势
Swift 3.0 answer: just handles does the vertical gesture
以下解决方案解决了我的问题:
这实际上只是检查平移是否在主视图或表视图上进行。
The following solution solved my problem:
This is actually just check if pan is going on main view or tableView.
Swift 3 版本 Lee 对懒人的回答
Swift 3 version of Lee's answer for the lazy
这是Swift 5中的自定义平移手势,
您可以限制其方向以及该方向上的最大角度,还可以限制其在该方向上的最小速度。
像这样调用:
或者
Here is a custom pan gesture in Swift 5
U can constraint its direction and the max angle in the direction, you can also constraint its minimum speed in the direction.
call like this:
or
我拿了Lee Goodrich的回答并扩展它,因为我特别需要一个单向平移。像这样使用它:
let pan = PanDirectionGestureRecognizer(direction: .vertical(.up), target: self, action: #selector(handleCellPan(_:)))
我还添加了一些注释来使其更清楚实际做出的决定是什么。
I took Lee Goodrich's answer and extended it as I needed specifically a single direction pan. Use it like this:
let pan = PanDirectionGestureRecognizer(direction: .vertical(.up), target: self, action: #selector(handleCellPan(_:)))
I also added some commenting to make it a little clearer what decisions are actually being made.
Swift 4.2
该解决方案仅支持垂直平移手势,与水平方向相同。
解决方案1:
解决方案2:
然后滑动手势会吞掉平移手势。当然,您不需要在
swipeAction
中执行任何操作。Swift 4.2
The solution is just for only support pan gesture vertically, same as horizontal.
Solution 1:
Solution 2:
Then the swipe gesture will swallow the pan gesture. Of course, you don't need to do anything in
swipeAction
.您可以通过
UIPanGestureRecognizer
找到在UIView
上拖动的方向。请按照代码操作。You can find the direction dragging on
UIView
throughUIPanGestureRecognizer
. Please follow the code.我的解决方法如下:
首先,我启用了同时平移手势识别。
然后我隔离水平和平移手势(累加器是 NSMutableArray 属性):
我在这里推送了一个示例:
在滚动视图中添加自定义平移
Here is how I resolved:
First I enabled Simultaneously PanGesture Recognition.
Then I Isolate Horizontal and Vertical Pan gestures (accumulator is NSMutableArray property):
I pushed an example here:
add custom pan in scrollview
您可以使用简单的
panGestureRecognizer
。无需使用pandirectionregognizer
或其他东西。只需使用translationInview
的 y 值下面的代码仅上下移动拖动视图
You may use simple
panGestureRecognizer
. No need to usepandirectionregognizer
or stuff. Just use y value oftranslationInview
Below code move drag view only up and down
我为只需要水平滚动的对象创建了 PanGestureRecognizer @selector 操作方法。
I created PanGestureRecognizer @selector action method for the object that needed only Horizontal scrolling.
快捷方式
Swift way
对于所有 Swift 用户来说,这将完成这项工作:)
For all you Swift users out there, this will do the job :)
我采用了 Lee Goodrich 的出色答案,并将其移植到 Swift 3
I took an excellent answer by Lee Goodrich and ported to Swift 3
我很乐意分享我的方法,因为所有其他方法都基于
UIGestureRecognizerDelegate
或子类UIPanGestureRecognizer
。我的方法基于运行时和调整。我对这种方法不是 100% 确定,但您可以自己测试和改进。
只需一行代码即可设置任何
UIPanGestureRecognizer
的方向:使用
pod 'UIPanGestureRecognizerDirection'
或代码:I would love to share my approach because all other approaches are based on either
UIGestureRecognizerDelegate
or subclassingUIPanGestureRecognizer
.My approach is based on runtime and swizzling. I'm not 100% sure about this approach, but you can test and improve it yourself.
Set the direction of any
UIPanGestureRecognizer
with just one line of code:use
pod 'UIPanGestureRecognizerDirection'
or the code:我尝试过这个:根据问题描述,这对我有用
I tried this: which worked for me as per the question describes
SWIFT 4.2
我更进一步,做了一个方向平移手势:(
使用:https://github.com/fastred/SloppySwiper 和 https://stackoverflow.com/a/30607392/5790492)
SWIFT 4.2
I went further and make a direction Pan Gesture:
(Used: https://github.com/fastred/SloppySwiper and https://stackoverflow.com/a/30607392/5790492)
PanGestureRecognizer
接口包含以下定义:我没有检查这一点,但也许可以通过子类访问它。
PanGestureRecognizer
interface contains the following definitions:I didn't check this, but maybe it's accessible via subclass.