How to animate left/right swipe view using UISwipeGestureRecognizer

Ujesh Patel picture Ujesh Patel · Sep 2, 2014 · Viewed 7.9k times · Source

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

}

Answer

Saheb Roy picture Saheb Roy · Sep 2, 2014

Suppose you want to animate the UITextField. And your UITextField has a CGRect with starting point of x - 50, y - 100;

   @implementation myViewController{
   UITextField *myTextField; //Declaring a textfield as an instance variable (global).
 }


    -(void)viewDidLoad{
    myTextField = [UITextField alloc]initWithFrame:CGRectMake(50,100,100,50)];//adding memory and setting its initial position
    //your other codes.
}
   -(void)screenswipeleft {
   [UIView animateWithDuration:3 animations:^{
        myTextField.frame = CGRectMake:(-75,100,100,50); //setting the same textfield in a position of -75 only showing 1/4th of the textfield. 
//this block animation would make the textfield animate from its starting position to this position in 3 seconds. (animateWithDuration:3 here u mention its duration).
    }];
   }

Apply same logic to others and you will get the animation you want.