简体   繁体   English

iOS创建滑动返回

[英]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. 我想像iOS 7中那样进行反击。我对整个iOS开发仍然是新的,这就是我目前使用的。
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. 我希望以前的视图跟随手指在平移手势上,而不是只是将pop调用到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). 简单来说,你有一个视图控制器,它可以容纳2个视图控制器(左边是视图1,右边是视图2)。

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). 例如,如果用户向右平移,则将视图2向右移动,并从左侧显示视图1(根据需要调用子视图控制器方法)。

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. 例如,如果您在屏幕上的视图1的90%右侧完成平移,则应在屏幕上完全移动视图1并在屏幕上查看2。 if you finish with 50% of each, you should use the direction of the pan to decide which view will remain on screen. 如果你用50%的每个完成,你应该使用平移的方向来决定哪个视图将保留在屏幕上。

使用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. 我想在这种情况下你应该使用UIScrollView,将滚动视图的内容大小设置为width_of_view * numberOfViews并将所有视图水平添加到滚动视图。 when user scroll or swipe their finger over screen than show the next view. 当用户在屏幕上滚动或滑动手指而不是显示下一个视图时。 here is one Link of apple Doc will help you. 这里有一个Apple Doc链接可以帮到你。 don't forget to set horizontal scrolling enable in XIB or in code. 不要忘记在XIB或代码中设置水平滚动启用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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