如何确定 UIPanGesture 的方向(向上或向下)? [英] How do I determine the direction (up or down) of a UIPanGesture?

查看:29
本文介绍了如何确定 UIPanGesture 的方向(向上或向下)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My goal is to set the iPhone's brightness with up or down pan gestures. If there is a simple way to determine the pan's direction, I can't figure it out. I added a swipe gesture recognizer, then the following code:

- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer           shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer     *)otherGestureRecognizer
{
    return YES;
}

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

but my pan: method never seems to be able to determine the direction of the pan. I've set "self" as the delegates for both gesture recognizers. The code that follows is my latest of many different attempts. (It works beautifully when panning down - the screen dims slowly or quickly, depending on the speed of the downward pan.)

- (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");
    }
   } 
}      

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

The output below represents a continuous upward pan of about half an inch. (pan: incorrectly thinks it's a downward 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)

我希望我没有让这变得比实际更难.以某种方式能够确定平移手势的方向(向上或向下)会更清晰,甚至不必为滑动手势而烦恼.

I hope I'm not making this harder than it really is. It would be much cleaner to somehow be able to determine the direction (up or down) of the pan gesture, and not have to even bother with swipe gestures.

任何帮助将不胜感激.

推荐答案

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

Perseverance pays off. I found the answer myself. Here's the code to determine if panning up or down. Just change the if statement to test velocity.x if you want to know if the direction is left or right.

- (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 的方向(向上或向下)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆