ios以任何角度向任何方向实施UISwipeGestureRecogniser

问题描述 投票:5回答:2

我试图在左下位置(左右之间的平均值)滑动东西。但UISwipeGesture仅识别左,右,下和上。

我想在我的整个屏幕视图上添加手势。然后,每当用户在屏幕上滑动时,我想知道滑动的起始和终止坐标。滑动可以是任何角度和任何位置。有没有办法在iOS中获得滑动的起点和终点坐标

请帮我。我被困住了。提前致谢。

ios uigesturerecognizer implementation gesture direction
2个回答
4
投票

现在我使用以下步骤完成了这个实现: -

扩展类“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;

1
投票

好吧,一条路线就是使用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度的公差角度,这允许在几乎每个方向上进行滑动检测。

© www.soinside.com 2019 - 2024. All rights reserved.