【问题标题】:ios Implement UISwipeGestureRecogniser in any direction at any angleios 任意角度任意方向实现UISwipeGestureRecogniser
【发布时间】:2013-08-27 11:46:46
【问题描述】:

我试图在左下位置滑动东西(指在左和下之间)。但 UISwipeGesture 只能识别左、右、下和上。

我想在整个屏幕视图上添加手势。然后,每当用户在屏幕上滑动时,我想知道滑动的开始和结束坐标。滑动可以是任何角度和任何位置。有什么方法可以在 iOS 中获取滑动的开始和结束坐标

请帮助我。我被严重卡住了。提前谢谢。

【问题讨论】:

  • 您可以使用平移手势而不是滑动手势,您可以在起点和终点根据坐标获取方向。或者最好的方法是在 View 上使用 touches 方法。

标签: ios uigesturerecognizer implementation gesture direction


【解决方案1】:

现在我通过以下步骤完成了这个实现:-

您可以在扩展类“UIGestureRecognizer”后实现一些委托方法。

这里是方法。

// 它会给出触摸的起点

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 CGPoint startSwipePoint= [touches.anyObject locationInView:self.view];
}

// 它会定期给出接触点

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
 CGPoint point=[touches.anyObject locationInView:self.view];
}

//它会给出结束滑动点

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{  
    CGPoint endSwipePoint= [touches.anyObject locationInView:self.view];
}

// 这是计算两点之间角度的代码。我正在使用起点和终点。但是你可以根据你的要求使用这两点中的任何一个

CGFloat angle=atan2(startSwipePoint.y - endSwipePoint.y, endSwipePoint.x - startSwipePoint.x) * Rad2Deg;

【讨论】:

    【解决方案2】:

    嗯,一种方法是只使用 UIPanGestureRecognizer。平移手势不关心他们前进的方向,并且能够在视图中跟踪他们的位置。以下是如何获得这些分数的示例。 (忽略明显的范围/存储问题)

    - (void)panDetected:(UIPanGestureRecognizer *)gesture
    {
        if (gesture.state == UIGestureRecognizerStateBegan) {
            CGPoint startingPoint = [gesture locationInView:gesture.view];
        }else if (gesture.state == UIGestureRecognizerStateEnded) {
            CGPoint endPoint = [gesture locationInView:gesture.view];
        }
    }
    

    但是,如果您不喜欢使用滑动手势识别器,您可以修改我在此处的链接答案中创建的一个几乎完整的对角滑动手势子类:How to make a combined gestures?

    这个子类允许您指定一个可以设置为 44.99 度的容差角度,这将允许几乎每个方向的滑动检测。

    【讨论】:

    • UIPanGesture 要求对象先获得。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    • 2020-07-04
    • 1970-01-01
    相关资源
    最近更新 更多