【问题标题】:Detect user touch movement direction?检测用户触摸移动方向?
【发布时间】:2016-06-10 07:10:51
【问题描述】:

我使用UIBezierPath 绘制了字母A。它有三个方向来演示用户将如何绘制这个字母表。有什么方法可以检查用户的触摸移动方向是否相同?

【问题讨论】:

  • 你可以从 UITouch 获取位置(CGPoint)。
  • @SaurabhPrajapati 是的,我可以做到,但我想知道如何检查用户是否朝同一个方向移动?
  • 将所有方向作为数据库。做一个阈值距离(20-30 像素),保持touchesBegan 点的活动。在每个touchesMoved 上计算距离和角度(起点和当前点)。比较和决定。
  • 用物理引擎试试这个?
  • @Saurabh 哪个物理引擎?物理引擎会给我用户移动的方向吗?我正在开发的所有应用程序都是 UIKit 基础

标签: ios drawing core-graphics uibezierpath uitouch


【解决方案1】:

我推荐UIPanGestureRecognizer。它将为您提供当前位置、从起点的平移和当前速度。如果您跟踪速度,您将知道它是否与之前的速度匹配。

可以使用-[UIBezierPath containsPoint:]判断手势是否还在A内。

像这样:

- (void)panRecognizerFired:(UIPanGestureRecognizer *)panRecognizer {
    switch (panRecognizer.state) {
        case UIGestureRecognizerStatePossible:
        {
            // ignore it until it changes to UIGestureRecognizerStateBegan
            return;
        }
        case UIGestureRecognizerStateBegan:
        {
            // handle gesture start here

            // change this to a break if you want a beginning touch to be processed
            // the same as a movement
            return;
        }
        case UIGestureRecognizerStateEnded:
        {
            // handle gesture end here - clean up and undo whatever happened
            // in UIGestureRecognizerStateBegan, show a new controller or something, etc

            // change this to a break if you want an ending touch to be processed
            // the same as a movement
            return;
        }
        case UIGestureRecognizerStateFailed:
        case UIGestureRecognizerStateCancelled:
        {
            // abort! undo everything here
            return;
        }
        default:
            // it moved! break to process that movement
            break;
    }

    CGPoint location = [panRecognizer locationInView:self.theViewThatHasTheLetterAInIt];
    CGPoint translation = [panRecognizer translationInView:self.theViewThatHasTheLetterAInIt];
    CGPoint velocity = [panRecognizer velocityInView:self.theViewThatHasTheLetterAInIt];

    BOOL isLocationInsidePath = [self.letterABezierPath containsPoint:location];

    // make decisions based on isLocationInsidePath and velocity here...
}

为了确保这个手势只允许在正确的数字附近开始,使自己符合UIGestureRecognizerDelegate协议并设置self.panGestureRecognizer.delegate = self。然后你可以用这个限制起始位置:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer == self.panGestureRecognizer) {
        CGPoint location = [gestureRecognizer locationInView:self.theViewThatHasTheLetterAInIt];

        // implement this property to return the center of the 1, 2, or 3 circle
        CGPoint targetCenter = self.currentTargetCenter;

        CGFloat distanceX = location.x - targetCenter.x;
        CGFloat distanceY = location.y - targetCenter.y;
        CGFloat distanceDiagonalSquared = (distanceX * distanceX) + (distanceY * distanceY);

        const CGFloat maxDistance = 20.f;

        return (distanceDiagonalSquared <= maxDistance * maxDistance);
    }

    return YES;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    相关资源
    最近更新 更多