【问题标题】:Get a vector from a UIPanGestureRecognizer从 UIPanGestureRecognizer 获取向量
【发布时间】:2013-01-14 12:55:12
【问题描述】:

我正在尝试从UIPanGestureRecognizer 中获取一个vector,它围绕Portrait Orientation 中的屏幕轴。我知道我可以得到速度,这是战斗的一半,但理想情况下我想要一种获得关于屏幕垂直轴的角度(罗盘度或弧度)的方法。例如,从左下角到右上角的拖动是 45 度角,从上到下是 180 度,或者从下到上是 0(或 360)。

这可能吗?它没有方向属性,文档中对翻译的解释有点混乱。我是否需要手动创建一个中心点(关于我的视图中心),并将Pan Touch 的起点/终点与该点进行比较?我对所需的数学有点不确定。

提前致谢! ^_^

【问题讨论】:

    标签: ios objective-c cocoa-touch uigesturerecognizer uipangesturerecognizer


    【解决方案1】:

    如果你能得到速度:

    CGPoint velocity = [recognizer velocityInView:self.view]
    

    然后您可以计算相对于 x 轴的角度:

    float x = velocity.x;
    float y = velocity.y;
    
    double angle = atan2(y, x) * 180.0f / 3.14159f;
    if (angle < 0) angle += 360.0f; 
    

    【讨论】:

    • 您能解释一下这段代码是如何工作的吗?我不太确定它在做什么。
    • 似乎这段代码有效,但 0 度是水平轴(所以如果我从左向右滑动,我得到 0 度)。我可以将其更改为从上到下为 0 度吗?
    【解决方案2】:
    CGPoint startLocation = [sender locationInView:self.view];
    if (sender.state == UIGestureRecognizerStateBegan) {
         startLocation = [sender locationInView:self.view];
    }
    else if (sender.state == UIGestureRecognizerStateEnded) {
         CGPoint stopLocation = [sender locationInView:self.view];
         CGFloat dx = stopLocation.x - startLocation.x;
         CGFloat dy = stopLocation.y - startLocation.y;
    
         double angle = atan2(dy, dx) * 180.0f / M_PI;
         if (angle < 0) angle += 360.0f;
         NSLog(@"angle: %f",angle);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多