简体   繁体   中英

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,

SecondView在视图之间滑动的firstView

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. Eg 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. eg 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.

使用IIViewDeckController它更容易,它可以做你想要的IIViewDeck GitHub

I think in this case you should use UIScrollView, set the content size of your scroll view to width_of_view* numberOfViews and add all your view to the scroll view horizontally. when user scroll or swipe their finger over screen than show the next view. here is one Link of apple Doc will help you. don't forget to set horizontal scrolling enable in XIB or in code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM