UIPanGestureRecognizer 起点已关闭
我有一个 UIView,它附加了一个 UIPanGestureRecognizer,手势工作正常,只是起点不是平移第一次开始的位置,它通常在 x 和 y 坐标中偏离 5 到 15 个像素。不幸的是,方差不是一致并且似乎与平移运动发生的速度有关。
为了验证触摸是否正确发送,我向子视图添加了 TouchBegan 方法,它接收正确的起始点,但手势在其开始阶段未提供相同的点。我的日志中的一些示例如下:“线条起点”是从手势识别器接收到的第一个点。
touchesBegan got point 617.000000x505.000000
Line start point at 630.000000x504.0000001
touchesBegan got point 403.000000x503.000000
Line start point at 413.000000x504.000000
touchesBegan got point 323.000000x562.000000
Line start point at 341.000000x568.000000
以前有人见过这个问题吗?
关于如何解决这个问题而无需实现全新的 UIGestureRecognizer 有什么想法吗?
I have a UIView that has a UIPanGestureRecognizer attached to it the gesture is working fine except that the starting point is not where the pan first started it is usually off by 5 to 15 pixels in both the x and y coordinates.Unfortunately the variance is not consistent and seems to be related to the speed at which the panning motion takes place.
To validate that the touches are being sent correctly I have added a touchesBegan method to a subview and it receives the correct starting point but the gesture does not provide the same point in it's begin phase. Some examples from my logs are below 'Line start point' is the first point received from the gesture recognizer.
touchesBegan got point 617.000000x505.000000
Line start point at 630.000000x504.0000001
touchesBegan got point 403.000000x503.000000
Line start point at 413.000000x504.000000
touchesBegan got point 323.000000x562.000000
Line start point at 341.000000x568.000000
Has anyone seen this issue before?
Any ideas on how to work around the issue with out having to implement an entirely new UIGestureRecognizer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用手势识别器的委托方法检测手势的初始触摸位置
You can detect initial touch position of a gesture with the gesture recognizer's delegate method
将此代码放入 UIGestureRecognizerStateBegan 案例中
Put this code in the UIGestureRecognizerStateBegan case
文档说平移当手指“移动到足以被视为平底锅”时,手势就开始了。此移动是为了区分按下和拖动,因为用户的手指在尝试按下而不拖动时可能会稍微移动。
我认为这就是您在第一个触摸点和第一个被视为拖动部分的点之间看到的差异。
The documentation says that the pan gesture starts when the fingers have "moved enough to be considered a pan." This movement is to distinguish a press from a drag, since the user's finger could move around a bit while they are trying to press without dragging.
I think this is the difference you're seeing between the first touch point and the first point considered part of the drag.
是的,区别在于手势识别器在激活之前会等待一段不确定的移动距离。您可以做的是创建自己的 UIPanGestureRecognizer 并在 TouchMoved 重写方法中将状态设置为 UIGestureRecognizerStateChanged。
注意:我使用 touhcesMoved 而不是 TouchBegan,因为我希望它在用户触摸移动时启动,而不是立即启动。
以下是执行此操作的自定义手势识别器的一些示例代码:
Yes the difference is because the gesture recognizer waits a undetermined distance of movement before becoming active. What you can do is create your own UIPanGestureRecognizer and set the state to UIGestureRecognizerStateChanged in the touchesMoved override method.
NOTE: I used touhcesMoved instead of touchesBegan because I wanted it to start when the users touch moved and not instantly.
Here is some example code for a custom gesture recognizer that does this:
为了解决这个问题,您可以尝试在手势识别器开始时重置翻译点。例如,像这样开始你的操作方法:
To get around this you could try reseting the translation point when the gesture recognizer begins. For example, start your action method out like so:
起点改变的原因正如@Douglas所说:
起点和平移都是在识别平移手势后计算的。
我使用以下方法来获取“真实”起点:
对于具有平移手势的视图,覆盖
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *) event
方法,并存储“真实”起点以供以后使用:The start point changing reason is as @Douglas said:
And the starting point and translation are both calculated after the pan gesture is recognized.
I use the following way to get the "real" start point:
For the view which has the pan gesture, override the
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
method, and store the "real" starting point for later use: