pound Pound
pound Pound

Reputation: 123

swipe only vertical/horizontal by standard distance

I need to move an object on a grid board, by a standard distance (eg 1 unit horizontally or vertically), depending on whether I swipe one finger horizontally or vertically. The distance is fixed as I mentioned and the direction will depend on whether I move from bottom to top, from left to right or vice versa.

it will be more like this game: https://www.youtube.com/watch?v=SuD_Ripl-MU

I make this code but it only detect left/right:

@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);
    }
}

How can I add gesture up/down and implementing swipe in smooth gesture (on my code it's move instantly to new location, what I want object move to new location if I release my finger from screen)?

Upvotes: 0

Views: 386

Answers (2)

if-else-switch
if-else-switch

Reputation: 977

UIPanGestureRecognizer is alone enough to do this task. Just check the translation difference in statebegan and stateEnd property of UIPanGesture and translate your view on the basis of velocity parameter. like -

if(velocity > 0) {

//your down/right movement

}
else {

//your left or up movement

}

Upvotes: 0

santhu
santhu

Reputation: 4792

UISwipeGestureRecognizerDirection enums

typedef enum {
   UISwipeGestureRecognizerDirectionRight = 1 << 0,
   UISwipeGestureRecognizerDirectionLeft  = 1 << 1,
   UISwipeGestureRecognizerDirectionUp    = 1 << 2,
   UISwipeGestureRecognizerDirectionDown  = 1 << 3
} UISwipeGestureRecognizerDirection;

I believe you had to add two extra swipeGestureRecognisers having directions UISwipeGestureRecognizerDirectionUp and UISwipeGestureRecognizerDirectionDown. That would solve your problem.

For example, for swipe-up

@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.
   }
}

Upvotes: 1

Related Questions