iOS创建滑动返回

标签 ios uinavigationcontroller uipangesturerecognizer uiswipegesturerecognizer ios7

我想像 iOS 7 中那样进行向后滑动。我对整个 iOS 开发还是陌生的,这就是我目前正在使用的。
目前我有一个平移手势,可以检测用户是否向后滑动,然后它只会弹出导航 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];
    }
}

我希望之前的 View 跟随手指的平移手势,而不是仅仅调用 pop 到 root。例如,

SecondView Swiping between the views FirstView

最佳答案

这将需要一个自定义容器 View Controller 。简单来说,您有一个 View Controller ,它包含 2 个 View Controller (左侧的 View 1 和右侧的 View 2)。

您将平移手势附加到容器 View ,当用户移动时,您为每个 subview Controller 计算适当的帧。例如。如果用户向右平移,您会将 View 2 移到右侧,并将 View 1 从左侧移入(根据需要调用 subview Controller 方法)。

当手势完成时,您应该结合平移的最终方向检查最终位置,以决定放置 View Controller 的位置。例如如果您在屏幕上 View 1 的 90% 处完成向右平移,则应将 View 1 完全移动到屏幕上并将 View 2 移出屏幕。如果你完成了每个 View 的 50%,你应该使用平移的方向来决定哪个 View 将保留在屏幕上。

关于iOS创建滑动返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17564800/

相关文章:

ios - 有什么方法可以更改所有 UINavigationBar 颜色?

ios - (使用 Swift 进行 UI 测试)获取事件应用程序列表失败 : AX error -25205

ios - 类型 'ViewController' 不符合协议(protocol)

ios - UICollectionView 仅防止第一个单元格重复使用单元格

ios - 为什么平移让用户将缩放 View 移到 super View 之外?

ios - UIImageView 的移动滞后

uiviewcontroller - 使用 present modally 将数据从一个 uiviewcontroller 发送到另一个 navigationcontroller

ios - ENsideMenuNavController - 禁用与所有其他 View 的交互

ios导航 Controller 推送 View Controller 但有后退按钮指向其他地方

ios - 如何在 iOS 的 ImageView 中添加手势识别器?