Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS creating swipe to go back

I want to make a back swipe like the one in iOS 7.I'm still new with the whole iOS development, this is what I'm currently using.
Currently I have a pan gesture that detects if the user swipes back and then it just pops the navigation controller.

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self.view addGestureRecognizer:pan];


-(void)handlePan:(UIPanGestureRecognizer *)sender{
    CGPoint tran = [recognizer translationInView:recognizer.view];
    CGPoint vel = [recognizer velocityInView:recognizer.view];
    if(vel.x > 500 && tran.x > 100){
        [self.navigationController popViewControllerAnimated:YES];
    }
}

I want the previous view to follow the finger on the pan gesture instead of just calling the pop to root. For example,

SecondViewSwiping between the viewsFirstView

like image 454
user1838169 Avatar asked Dec 26 '22 00:12

user1838169


1 Answers

This will need a custom container view controller. In simple terms you have a view controller which holds 2 view controllers (View 1 on the left and View 2 on the right).

You attach a pan gesture to the containers view, and when the user moves, you calculate the appropriate frame for each of the sub view controllers. E.g. if the user is panning to the right, you will move the view 2 off to the right, and bring view 1 in from the left (calling the child view controller methods as needed).

When the gesture finishes you should check the final position in combination with the final direction of the pan to decide where you should place the view controllers. e.g. if you finish panning ot the right at 90% of view 1 on screen, you should move view 1 fully on screen and view 2 off screen. if you finish with 50% of each, you should use the direction of the pan to decide which view will remain on screen.

like image 131
stevo- Avatar answered Jan 08 '23 12:01

stevo-