【问题标题】:swipe only vertical/horizontal by standard distance仅垂直/水平滑动标准距离
【发布时间】:2014-03-19 18:06:59
【问题描述】:

我需要将网格板上的对象移动标准距离(例如水平或垂直1个单位),具体取决于我是水平还是垂直滑动一根手指。正如我提到的,距离是固定的,方向取决于我是从下到上,从左到右还是从左到右。

它会更像这个游戏: https://www.youtube.com/watch?v=SuD_Ripl-MU

我编写了这段代码,但它只检测左/右:

@interface ViewController()
@property (nonatomic, strong) UISwipeGestureRecognizer *leftSwipeGestureRecognizer;
@property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipeGestureRecognizer;
@end

@implementation ViewController

- (IBAction)HandlePan:(UIPanGestureRecognizer *)recognizer{
    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0,0) inView:self.view];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];

    self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;

    [self.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
    [self.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
}

- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
{
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        CGPoint labelPosition = CGPointMake(self.pesawat.frame.origin.x - 30.0, self.pesawat.frame.origin.y);
        self.pesawat.frame = CGRectMake( labelPosition.x , labelPosition.y , self.pesawat.frame.size.width, self.pesawat.frame.size.height);
     }

    if (sender.direction == UISwipeGestureRecognizerDirectionRight)
    {
        CGPoint labelPosition = CGPointMake(self.pesawat.frame.origin.x + 30.0, self.pesawat.frame.origin.y);
     self.pesawat.frame = CGRectMake( labelPosition.x , labelPosition.y , self.pesawat.frame.size.width, self.pesawat.frame.size.height);
    }
}

如何添加向上/向下手势并以平滑手势实现滑动(在我的代码中,它会立即移动到新位置,如果我从屏幕上松开手指,我希望对象移动到新位置)?

【问题讨论】:

    标签: ios iphone objective-c swipe


    【解决方案1】:
    UISwipeGestureRecognizerDirection enums
    
    typedef enum {
       UISwipeGestureRecognizerDirectionRight = 1 << 0,
       UISwipeGestureRecognizerDirectionLeft  = 1 << 1,
       UISwipeGestureRecognizerDirectionUp    = 1 << 2,
       UISwipeGestureRecognizerDirectionDown  = 1 << 3
    } UISwipeGestureRecognizerDirection;
    

    我相信你必须添加 两个额外的 swipeGestureRecognisers 有方向 UISwipeGestureRecognizerDirectionUpUISwipeGestureRecognizerDirectionDown。那会解决你的问题。

    例如,向上滑动

    @property (nonatomic, strong) UISwipeGestureRecognizer *upSwipeGestureRecognizer;
    
    self.upSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    self.upSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
    
    
    - (void)handleSwipes:(UISwipeGestureRecognizer *)sender
    {
       .....
       if (sender.direction == UISwipeGestureRecognizerDirectionUp)
       {
         //add your code here.
       }
    }
    

    【讨论】:

      【解决方案2】:

      UIPanGestureRecognizer 可以单独完成这项任务。只需检查 UIPanGesture 的 statebegan 和 stateEnd 属性的翻译差异,并根据速度参数翻译您的视图。 喜欢 -

      if(velocity > 0) {
      
      //your down/right movement
      
      }
      else {
      
      //your left or up movement
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-25
        • 2017-06-30
        • 1970-01-01
        • 2016-10-07
        • 1970-01-01
        相关资源
        最近更新 更多