IOS gesture sliding between left and right
1. Add two gesture properties in "ViewController. h.
?
1 2 |
@property (nonatomic, strong) UISwipeGestureRecognizer *leftSwipeGestureRecognizer; @property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipeGestureRecognizer; |
And synthesize to the "ViewController. m" file.
2. Add the following code to the "ViewDidLoad" method in the "ViewController. m" file:
?
1 2 3 4 5 6 7 8 |
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]; |
3. Add the following method in "ViewController. m;
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender { if (sender.direction == UISwipeGestureRecognizerDirectionLeft) { CGPoint labelPosition = CGPointMake(self.swipeLabel.frame.origin.x - 100.0, self.swipeLabel.frame.origin.y); self.swipeLabel.frame = CGRectMake( labelPosition.x , labelPosition.y , self.swipeLabel.frame.size.width, self.swipeLabel.frame.size.height); self.swipeLabel.text = @ "Nima, you are running on the left ...." ; } if (sender.direction == UISwipeGestureRecognizerDirectionRight) { CGPoint labelPosition = CGPointMake(self.swipeLabel.frame.origin.x + 100.0, self.swipeLabel.frame.origin.y); self.swipeLabel.frame = CGRectMake( labelPosition.x , labelPosition.y , self.swipeLabel.frame.size.width, self.swipeLabel.frame.size.height); self.swipeLabel.text = @ "Nima, you are running to the right ...." ; |