将 UILongPressGestureRecognizer 与 UIPanGestureRecognizer 相结合
我想将 UILongPressGestureRecognizer 与 UIPanGestureRecognizer 结合起来。
UIPanGestureRecognizer 应通过长按启动。有没有一种简单的方法可以做到这一点?或者我真的必须编写自己的手势识别器吗?
我不想在主屏幕上出现类似的东西。您按下一个图标,一段时间后图标开始摆动。之后,无需将手指从屏幕上松开,我就可以开始拖动手指下的图标。
I d'like to combine a UILongPressGestureRecognizer with a UIPanGestureRecognizer.
The UIPanGestureRecognizer should start with a long press. Is there a simple way to do this? or do I really have to write my own gesture recognizer?
I wan't something like on the home screen. You press on an icon and after some time the icons start wobbling. Afterwards without releasing my finger from the screen I can start dragging the icon under my finger around.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
实际上,您不必组合手势识别器 - 您可以仅使用 UILongPressGestureRecognizer 来完成此操作...一旦您的触摸在“allowableMovement”内停留了“minimumPressDuration”,您就进入 StateBegan。只要不抬起任何手指,您就可以保持连续的长按手势 - 这样您就可以开始移动手指并通过 StateChanged 跟踪移动。
长按手势是连续的。当允许的手指数量 (numberOfTouchesRequired) 已按下指定时间段 (minimumPressDuration) 并且触摸移动未超出允许的移动范围 (allowableMovement) 时,手势开始 (UIGestureRecognizerStateBegan)。每当手指移动时,手势识别器就会转换为 Change 状态,并在任何手指抬起时结束 (UIGestureRecognizerStateEnded)。
actually, you don't have to combine gesture recognizers - you can do this solely with UILongPressGestureRecognizer... You enter StateBegan once your touch(es) have stayed within 'allowableMovement' for 'minimumPressDuration'. You stay in your continuous longPressGesture as long as you don't lift any of your fingers - so you can start moving your fingers and track the movement through StateChanged.
Long-press gestures are continuous. The gesture begins (UIGestureRecognizerStateBegan) when the number of allowable fingers (numberOfTouchesRequired) have been pressed for the specified period (minimumPressDuration) and the touches do not move beyond the allowable range of movement (allowableMovement). The gesture recognizer transitions to the Change state whenever a finger moves, and it ends (UIGestureRecognizerStateEnded) when any of the fingers are lifted.
我为这个问题感到有点为难。接受的答案还不够。无论我在该方法中输入什么,平移或长按处理程序都会被调用。我找到的解决方案如下:
将以下委托方法添加到您的类中(根据上面的答案):
将以下委托方法添加到您的类中:
然后添加一个属性或 ivar,用于跟踪是否应允许开始平移(请参阅上面的方法)。在我的例子中,
BOOL shouldAllowPan
。在
init
或viewDidLoad
中将 BOOL 设置为NO
。在 longPress 处理程序中将 BOOL 设置为 YES。我这样做:在 panHandler 内部,我对 BOOL 进行检查:
最后在 panHandler 中重置 BOOL:
然后去喝杯啤酒祝贺一下自己。 ;)
I had a bit of a hard time for this problem. The accepted answer wasn't enough. No matter what I put in that method the pan or longpress handlers would get invoked. A solution I found was as follows:
UIGestureRecognizerDelegate
.Add the following delegate method to your class (as per the answer above):
Add the following delegate method to your class:
Then add either a property or ivar which will track if the pan should be allowed to begin (see method above). In my case
BOOL shouldAllowPan
.Set the BOOL to
NO
in yourinit
orviewDidLoad
. Inside your longPress handler set the BOOL toYES
. I do it like this:Inside the panHandler I do a check on the BOOL:
And finally reset the BOOL within the panHandler:
And then go grab a beer to congratulate yourself. ;)
我找到了一个解决方案:
这个 UIGestureRecognizerDelegate 方法正是我所寻找的:
I found a solution:
This UIGestureRecognizerDelegate method does exactly what I looked for:
Andy B 在 Swift 中的方法,
将 UIGestureRecognizerDelegate 委托添加到类中
添加成员变量
添加手势,需要将平移手势委托添加到VC中。这是触发 shouldRecognizeSimultaneouslyWithGestureRecognizer 和gestureRecognizerShouldBegin 函数所必需的
允许同时手势
在长按处理程序内:
Andy B's approach in Swift,
Add the UIGestureRecognizerDelegate delegate to the class
Add a member variable
Add the gestures and need to add the pan gesture delegate to the VC. This is needed to fire off the shouldRecognizeSimultaneouslyWithGestureRecognizer and gestureRecognizerShouldBegin functions
Allow simultaneous gestures
Inside the long press handler:
要组合更多手势:
var shouldAllowSecondGesture : Bool = false
let longPressRec = UILongPressGestureRecognizer(target: self, action: #selector(self.startDrag(sender:) ))
cell.addGestureRecognizer(longPressRec)
让 panGestureRecognizer = UIPanGestureRecognizer(目标: self, 操作: #selector(self.handlePan(sender:)))
cell.isUserInteractionEnabled = true
cell.addGestureRecognizer(panGestureRecognizer)
扩展您的 VC 并实现 GestureRecognizerDelegate 来实现此方法。
扩展 YourViewController : UIGestureRecognizerDelegate {
For combinate more gesture :
var shouldAllowSecondGesture : Bool = false
let longPressRec = UILongPressGestureRecognizer(target: self, action: #selector(self.startDrag(sender:)))
cell.addGestureRecognizer(longPressRec)
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(self.handlePan(sender:)))
cell.isUserInteractionEnabled = true
cell.addGestureRecognizer(panGestureRecognizer)
Extension your VC and implement GestureRecognizerDelegate for implemented this method.
extension YourViewController : UIGestureRecognizerDelegate {
请阅读 Apple UIGestureRecognizer 类参考的“子类化注释”部分:
https://developer.apple.com/library/prerelease/tvos/documentation/UIKit/Reference/UIGestureRecognizer_Class/
Read the "Subclassing Notes" section of Apple's UIGestureRecognizer Class Reference at:
https://developer.apple.com/library/prerelease/tvos/documentation/UIKit/Reference/UIGestureRecognizer_Class/
我通过实现“操作:选择器?”所需的功能解决了这个问题。 “动作:选择器?”中 UIPanGestureRecognizer 的函数UILongPressGestureRecognizer 的 func。
由于“UILongPressGestureRecognizer”没有成员“翻译”,因此我通过保存原始触摸的位置并从实际触摸位置提取它来计算翻译。
I solved this issue by implementing the desired functionality of the "action: Selector?" func of the UIPanGestureRecognizer within the "action: Selector?" func for the UILongPressGestureRecognizer.
As 'UILongPressGestureRecognizer' has no member 'translation', I calculated the translation by saving the position of the original touch and them extracting it from the actual touch position.