Search code examples
iosxcodeanimationios7uiswipegesturerecognizer

How to animate left/right swipe view using UISwipeGestureRecognizer


try to make swipe in single-view and it's working but not get animation and page indicatore bubble in bottom

- (void)viewDidLoad
{
[super viewDidLoad];
UISwipeGestureRecognizer *swipeleft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(screenswipeleft)];
swipeleft.numberOfTouchesRequired=1;
swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeleft];

UISwipeGestureRecognizer *swiperight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(viewDidLoad)];
swiperight.numberOfTouchesRequired=1;
swiperight.direction=UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swiperight];

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
[self.view addSubview:textField];
NSLog(@"move left");

}
-(void)screenswipeleft {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
[self.view addSubview:textField];
NSLog(@"move right");

}

Solution

  • - (void)viewDidLoad
      {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.   
        _leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
        _rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    
        _leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
        _rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    
        [self.view addGestureRecognizer:_leftSwipeGestureRecognizer];
        [self.view addGestureRecognizer:_rightSwipeGestureRecognizer];
    }
    
    - (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);
    
        }
    
        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);
       }
    }