DYNavigationController是一个实现了左右滑动导航的项目。

https://github.com/dyang/DYNavigationController

首先用之前的跟视图初始化DYNavigationController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch.
RootViewController *rootViewController = [[[RootViewController alloc] init] autorelease];
DYNavigationController *navigationController = [[[DYNavigationController alloc]
initWithRootViewController:rootViewController] autorelease]; self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}

这个方法定义了左右滑动的手势操作

- (void)setUpGestureRecognizers:(UIViewController *)viewController {
// Swipe right: pop the current view and go back one level
UISwipeGestureRecognizer *rightSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(popCurrentViewOut:)];
rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
[viewController.view addGestureRecognizer:rightSwipeGesture];
[rightSwipeGesture release]; // Swipe left: push a new view
UISwipeGestureRecognizer *leftSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(pushNewViewIn:)];
leftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[viewController.view addGestureRecognizer:leftSwipeGesture];
[leftSwipeGesture release];
}

当向右滑动的时候,当前的视图会被移动的屏幕的左边,并加入到导航的栈里

- (void)pushViewController:(UIViewController *)viewController {
// Place the new view to the right next to the current view
viewController.view.frame = CGRectOffset(self.view.bounds, self.view.bounds.size.width, ); // Add the new view to our view hierarchy so that it displays on screen.
[self.view addSubview:viewController.view]; // Start animation
[UIView animateWithDuration:ANIMATION_DURATION delay:ANIMATION_DELAY options:UIViewAnimationCurveEaseInOut animations:^{
[self currentViewController].view.frame = CGRectOffset(self.view.bounds, -self.view.bounds.size.width, );
viewController.view.frame = self.view.bounds;
} completion:^(BOOL finished) {
if (finished) {
// Connect DYNavigationController to viewController if needed
[self setNavigatorIfNeeded:viewController]; // Set up gesture recognizer so that we can respond to swipes
[self setUpGestureRecognizers:viewController]; // Add the new controller to our viewControllerStack
[self.viewControllerStack addObject:viewController];
}
}];
}

这里使用了UIView的类方法animateWithDuration实现了跳转的动画。

当向左滑动的时候,当前视图被移除导航栈,左边的视图向右移到屏幕中央。

- (void)popViewController {
// Sanity check - We only pop when there are at least two viewControllers in the stack,
// otherwise there is nothing to pop
if (self.viewControllerStack.count < ) return; // Start animation
[UIView animateWithDuration:ANIMATION_DURATION delay:ANIMATION_DELAY options:UIViewAnimationCurveEaseInOut animations:^{
[self currentViewController].view.frame = CGRectOffset(self.view.bounds, self.view.bounds.size.width, );
[self previousViewController].view.frame = self.view.bounds;
} completion:^(BOOL finished) {
if (finished) {
// Tear down gesture recognizers
[self tearDownGestureRecognizers:[self currentViewController]]; // Remove current viewController.view from self.
[[self currentViewController].view removeFromSuperview]; // Remove current viewController from self.
[[self currentViewController] removeFromParentViewController]; // Remove current view controller from viewControllerStack
[self.viewControllerStack removeLastObject];
}
}];
}

导航的具体实现在DYNavigationController中,DYNavigationControllerDelegate持有一个DYNavigationController的property,在RootViewController中进行了synthesize,RootViewController实现了DYNavigationControllerDelegate协议,DetailViewController也实现了DYNavigationControllerDelegate协议,所以DetailViewController中可以直接使用这个DYNavigationController的引用,来实现导航功能。

最新文章

  1. C语言 &#183; 查找整数
  2. iscroll总结
  3. C#调用C/C++动态库 封送结构体,结构体数组
  4. prototype.js 和 jQuery.js中 ajax 的使用
  5. 一个fork()系统调用的问题
  6. Oracle 删除数据后释放数据文件所占磁盘空间
  7. 谁是最好的Coder
  8. Ubuntu Broadcom无线网卡驱动安装
  9. 很棒的jQuery代码片段分享
  10. 文本编辑BOM标记(Byte Order Mark)
  11. rz/sz上传下载
  12. java解析xml的几种方式
  13. python接口自动化(八)--发送post请求的接口(详解)
  14. 联发科Helio P90,MT6779VWB芯片处理器
  15. html2canvas截屏用法
  16. topcoder srm 635 div1
  17. vue error:The template root requires exactly one element.
  18. BZOJ3724 PA2014Final Krolestwo(欧拉回路+构造)
  19. 任意两点间的最短路问题(Floyd-Warshall算法)
  20. XML 文档(1, 2)中有错误:不应有 &lt;xml xmlns=&#39;&#39;&gt;

热门文章

  1. day5时间复杂度
  2. ASP.NET Zero--4.不使用谷歌字体,提升加载速度
  3. Windows Azure 初体验
  4. 如何才能通俗易懂地解释JS中的的&quot;闭包&quot;?
  5. 【UOJ】#79. 一般图最大匹配
  6. 查看loadrunner运行日志
  7. bzoj 1856 卡特兰数
  8. file.listFiles()按文件大小、名称、日期排序方法
  9. FastReport.Net使用:[10]报表栏目说明
  10. POWEROJ 2610 判断回文串 【HASH】