如何确定 UIPanGesture 的方向(向上或向下)?

Posted

技术标签:

【中文标题】如何确定 UIPanGesture 的方向(向上或向下)?【英文标题】:How do I determine the direction (up or down) of a UIPanGesture? 【发布时间】:2013-01-17 23:15:30 【问题描述】:

我的目标是通过向上或向下平移手势来设置 iPhone 的亮度。如果有一个简单的方法来确定锅的方向,我想不通。我添加了一个滑动手势识别器,然后是以下代码:

- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer           shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer     *)otherGestureRecognizer

    return YES;

但是我的 pan: 方法似乎永远无法确定锅的方向。我已将“self”设置为两个手势识别器的代表。下面的代码是我最近的许多不同尝试。 (向下平移时效果很好 - 屏幕变暗缓慢或快速,具体取决于向下平移的速度。)

- (IBAction)downSwipe:(UISwipeGestureRecognizer *)recognizer // 1/16/13

    NSLog (@"downSwipe");
    if ((recognizer.state == UIGestureRecognizerStateChanged) ||
    (recognizer.state == UIGestureRecognizerStateEnded))
  
    NSLog (@"downSwipe in downSwipe");
    self.brightness = self.brightness -.1;
    NSLog (@"Going Down");
  


- (IBAction)upSwipe:(UISwipeGestureRecognizer *)recognizer  // 1/16/13

    NSLog (@"upSwipe");
    if ((recognizer.state == UIGestureRecognizerStateChanged) ||
    (recognizer.state == UIGestureRecognizerStateEnded))
  
        NSLog (@"upSwipe in upSwipe");
        self.brightness = self.brightness +.1;
        NSLog (@"Going UP");
  


- (void)pan:(UIPanGestureRecognizer *)recognizer // 1/12/13


if ((recognizer.state == UIGestureRecognizerStateChanged) ||
    (recognizer.state == UIGestureRecognizerStateEnded))
   
    if (UISwipeGestureRecognizerDirectionDown)
    
        NSLog (@"downSwipe in pan");
        self.brightness = self.brightness -.005;
        NSLog (@"Going DOWN in pan");
    
     else if (UISwipeGestureRecognizerDirectionUp)
    
        NSLog (@"upSwipe in pan");
        self.brightness = self.brightness +.005;
        NSLog (@"Going UP in pan");
    
    
      

下面的输出表示大约半英寸的连续向上平移。 (pan:错误地认为它是一个向下的pan。)

2013-01-17 17:49:57.774 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.783 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.794 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.801 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.811 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.818 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.832 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.839 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.848 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.855 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.864 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.871 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.881 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.887 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.896 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.903 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.910 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.919 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.929 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.935 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.945 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.951 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.960 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.966 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.998 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:58.004 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:58.022 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:58.030 SeriousPanicButton[9426:707] Going DOWN in pan

Obviously this if statement is not working:

if (UISwipeGestureRecognizerDirectionDown)

我希望我不会让这件事变得比实际更难。以某种方式能够确定平移手势的方向(向上或向下)会更干净,甚至不必费心滑动手势。

任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:

坚持是有回报的。我自己找到了答案。 这是确定是向上还是向下平移的代码。如果您想知道方向是左还是右,只需将 if 语句更改为测试 velocity.x。

- (void)pan:(UIPanGestureRecognizer *)recognizer // 1/18/13

if ((recognizer.state == UIGestureRecognizerStateChanged) ||
    (recognizer.state == UIGestureRecognizerStateEnded))

    CGPoint velocity = [recognizer velocityInView:self.view];

    if (velocity.y >0)   // panning down
    
        self.brightness = self.brightness -.02;
   //     NSLog (@"Decreasing brigntness in pan");
    
    else                // panning up
    
    self.brightness = self.brightness +.02;
  //  NSLog (@"Increasing brigntness in pan");
    


【讨论】:

以上是关于如何确定 UIPanGesture 的方向(向上或向下)?的主要内容,如果未能解决你的问题,请参考以下文章

SpriteKit 中的 UIPanGesture

仅在 iPhone 上水平添加 UIPanGesture

使用自动布局约束时,UIView 无法通过 UIpangesture 顺利拖动

如何确定绝对方向

如何确定 onmousemove 事件的方向?

如何根据objective-c中的滚动方向创建一个从UINavBar后面向下或向上滑动的自定义UIView?