UIPanGestureRecognizer 起点已关闭

发布于 2024-09-02 01:35:00 字数 601 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦归所梦 2024-09-09 01:35:00

您可以使用手势识别器的委托方法检测手势的初始触摸位置

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

You can detect initial touch position of a gesture with the gesture recognizer's delegate method

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
池木 2024-09-09 01:35:00
  CGPoint beg = [panRecognizer locationInView:_scrollView];
  CGPoint trans = [panRecognizer translationInView:_scrollView];
  CGPoint firstTouch = CGPointSubtract(beg, trans);

将此代码放入 UIGestureRecognizerStateBegan 案例中

  CGPoint beg = [panRecognizer locationInView:_scrollView];
  CGPoint trans = [panRecognizer translationInView:_scrollView];
  CGPoint firstTouch = CGPointSubtract(beg, trans);

Put this code in the UIGestureRecognizerStateBegan case

无语# 2024-09-09 01:35:00

文档说平移当手指“移动到足以被视为平底锅”时,手势就开始了。此移动是为了区分按下和拖动,因为用户的手指在尝试按下而不拖动时可能会稍微移动。

我认为这就是您在第一个触摸点和第一个被视为拖动部分的点之间看到的差异。

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.

甜扑 2024-09-09 01:35:00

是的,区别在于手势识别器在激活之前会等待一段不确定的移动距离。您可以做的是创建自己的 UIPanGestureRecognizer 并在 TouchMoved 重写方法中将状态设置为 UIGestureRecognizerStateChanged。

注意:我使用 touhcesMoved 而不是 TouchBegan,因为我希望它在用户触摸移动时启动,而不是立即启动。

以下是执行此操作的自定义手势识别器的一些示例代码:

#import "RAUIPanGestureRecognizer.h"

@implementation RAUIPanGestureRecognizer 


#pragma mark - UIGestureRecognizerSubclass Methods

- (void)reset
    { [super reset ]; }

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
    { [super touchesBegan:touches withEvent:event ]; }

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    { 
        [self setState:UIGestureRecognizerStateChanged ];
        [super touchesMoved:touches withEvent:event ]; 
    }

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    { [super touchesEnded:touches withEvent:event ]; }

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    { [super touchesCancelled:touches withEvent:event ]; }


@end

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:

#import "RAUIPanGestureRecognizer.h"

@implementation RAUIPanGestureRecognizer 


#pragma mark - UIGestureRecognizerSubclass Methods

- (void)reset
    { [super reset ]; }

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
    { [super touchesBegan:touches withEvent:event ]; }

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    { 
        [self setState:UIGestureRecognizerStateChanged ];
        [super touchesMoved:touches withEvent:event ]; 
    }

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    { [super touchesEnded:touches withEvent:event ]; }

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    { [super touchesCancelled:touches withEvent:event ]; }


@end
各自安好 2024-09-09 01:35:00

为了解决这个问题,您可以尝试在手势识别器开始时重置翻译点。例如,像这样开始你的操作方法:

- (void)panGesture:(UIPanGestureRecognizer *)recognizer;
{
    if ( recognizer.state == UIGestureRecognizerStateBegan )
    {
        CGPoint point = ...; // The view's initial origin.
        UIView *superview = [recognizer.view superview];
        [recognizer setTranslation:point inView:superview];
    }
}

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:

- (void)panGesture:(UIPanGestureRecognizer *)recognizer;
{
    if ( recognizer.state == UIGestureRecognizerStateBegan )
    {
        CGPoint point = ...; // The view's initial origin.
        UIView *superview = [recognizer.view superview];
        [recognizer setTranslation:point inView:superview];
    }
}
何时共饮酒 2024-09-09 01:35:00

起点改变的原因正如@Douglas所说:

  • 当手指移动到足以被视为平移时,平移手势开始。

起点和平移都是在识别平移手势后计算的。

我使用以下方法来获取“真实”起点:

对于具有平移手势的视图,覆盖 -(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *) event 方法,并存储“真实”起点以供以后使用:

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    self.touchStartPoint = [[[touches allObjects] firstObject] locationInView:self];
}

The start point changing reason is as @Douglas said:

  • the pan gesture starts when the fingers have moved enough to be considered a pan.

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:

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    self.touchStartPoint = [[[touches allObjects] firstObject] locationInView:self];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文