本文写的是视图切换,涉及到的内容有

1.实现代码添加Navigation Bar  Toolbal;

2.实现在Navigation Bar和Toolbar上用代码添加Bar Button Item;

3.UIView层面的简单动画效果

先把实现结果功能截图贴出来,对应动画效果

开始界面 和第一次向上翻页

向上翻页 和向下翻页

从左向右翻页 和从右向左翻页

开始制作:

1.创建一个新工程叫NVDemo; File->New->Project ->single View Application -> next

2.在新建两个ViewController,分别为FirstViewController和SecondViewController,顺便把XIB一块生成好

3.首先在视图上添加导航栏和导航按钮,经测试导航栏上只能添加两个导航按钮,和设置一个title标题;

我们还需要知道的一个常识是NavigationBar  ToolBar  Tab  Bar  都是44像素,所以在设置他们宽度时候他们的高度设置成44

还有一个通知栏,显示电量信息信号的地方是20像素;

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. // Do any additional setup after loading the view, typically from a nib.
  5. //    创建导航栏,44像素
  6. UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
  7. //    需要在导航栏上创建按钮,所以先创建一个导航栏集合
  8. UINavigationItem *navagationItem = [[UINavigationItem alloc] initWithTitle:@"导航栏"];
  9. UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(pageDown:)];
  10. UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"右测试"
  11. style:UIBarButtonItemStyleDone
  12. target:self
  13. action:@selector(leftpage:)];
  14. [navigationBar pushNavigationItem:navagationItem animated:YES];
  15. [navagationItem setLeftBarButtonItem:leftButton animated:YES];
  16. [navagationItem setRightBarButtonItem:rightButton animated:YES];
  17. [self.view addSubview:navigationBar];
  18. UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 416, 320, 44)];
  19. NSMutableArray *toolBarArray = [NSMutableArray array];
  20. [toolBarArray addObject:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPageCurl
  21. target:self
  22. action:@selector(switchLoadView:)]];
  23. [toolBarArray addObject:[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
  24. target:self
  25. action:@selector(rightpage:)]];
  26. [toolBarArray addObject:[[UIBarButtonItem alloc]initWithTitle:@"MyTitle"
  27. style:UIBarButtonItemStylePlain
  28. target:self
  29. action:nil]];
  30. //UIToolBar上添加图像
  31. [toolBarArray addObject:[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"myImage.png"]
  32. style:UIBarButtonItemStylePlain
  33. target:self
  34. action:nil]];
  35. [toolBar setItems:toolBarArray animated:YES];
  36. [self.view addSubview:toolBar];
  37. }

[navigationBar pushNavigationItem:navagationItem animated:YES];涉及到一个压栈的操作,把navigationItem压到navigationBar里,导航栏上只能添加左右两个按钮;所以是setLeftBarButtonItem 和 setRightBarButtonItem,最后再将navigationBar添加到视图之上;

在Toolbar上添加可以添加各种按钮,创建一个可变数组,把添加的按钮全部放到数组上,[toolBar setItems:toolBarArrayanimated:YES];将数组里按钮集合添加到了toolBar上面,选取图片的时候素材没选好,所以显示出来的图片那个按钮效果不是太好

4.接下来说的是按钮事件,因为需要用到FirstViewController和SecondViewController,在RootViewController.m添加上他们的头文件,为了区别确实是两个视图的切换,在他们的ViewDidLoad函数中初始化视图的背景颜色,

self.view.backgroundColor = [UIColor yellowColor];  和self.view.backgroundColor = [UIColor redColor];

按钮时间再次也不做过多解释,全部写在注释里了,其他几个都一样,只是修改了动画效果,变化不大,详情可下载源代码研究一下;

  1. -(void) switchLoadView:(id)sender
  2. {
  3. //开始一个动画
  4. [UIView beginAnimations:@"Curl" context:nil];
  5. //    设置动画方式,开始和结束时动画效果比较慢
  6. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  7. //    动画持续时间
  8. [UIView setAnimationDuration:1.25];
  9. //    设置动画效果,向上翻页
  10. [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
  11. //    首先判断firstView视图父视图是否为空
  12. if (firstView.view.superview == nil)
  13. {
  14. //        父视图为空,在判断他的子视图是否为空,如果为空在创建一个视图加载上面
  15. if (firstView.view == nil) {
  16. FirstViewController *firstViewDemo = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
  17. firstView = firstViewDemo;
  18. }
  19. //        把seconView视图从父视图中移除
  20. [secondView.view removeFromSuperview];
  21. //        在当前视图插入子视图firstView的视图
  22. [self.view insertSubview:firstView.view atIndex:0];
  23. }
  24. else {
  25. if (secondView.view == nil)
  26. {
  27. SecondViewController *secondViewDemo = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
  28. secondView = secondViewDemo;
  29. }
  30. [firstView.view removeFromSuperview];
  31. [self.view insertSubview:secondView.view atIndex:0];
  32. }
  33. //       动画结束
  34. [UIView commitAnimations];
  35. }

附上代码:http://download.csdn.net/detail/duxinfeng2010/4407717

咱在这在研究一个问题,在RootAppDelegate.m中我们先看看系统生成的代码

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  4. // Override point for customization after application launch.
  5. self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
  6. self.window.rootViewController = self.viewController;
  7. [self.window makeKeyAndVisible];
  8. return YES;
  9. }

系统直接加载的就是的RootViewController的视图,也就是弹出第一个界面是RootViewController.xib,假如说我想第一个就显示的FirstViewController控制的视图怎么办? 我们就可以在这个函数中进行重写

在delegateApp.h中@class RootViewController后面添加@class FirstViewController;此处声明一个类,在这样当我们添加@property (strong, nonatomic) FirstViewController *fTestViewController;才不会报错;

只需修改两行代码,此处注释原先加载RootViewController视图的代码,可能是我命名的不够合理,RootViewController和rootViewController要区别开,一个工程建立的,一个是系统本身自动生成的

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  4. // Override point for customization after application launch.
  5. //    self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
  6. //    self.window.rootViewController = self.viewController;
  7. self.fTestViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
  8. //  把根控制视图设置成fTestViewController
  9. self.window.rootViewController = self.fTestViewController;
  10. [self.window makeKeyAndVisible];
  11. return YES;
  12. }

然后再到我们的FirstViewController.m的ViewDidLoad函数先添加几行代码,以示区别

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. // Do any additional setup after loading the view from its nib.
  5. self.view.backgroundColor = [UIColor yellowColor];
  6. UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
  7. NSMutableArray *toolBarArray = [NSMutableArray array];
  8. [toolBarArray addObject:[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction
  9. target:self
  10. action:nil]];
  11. UIBarButtonItem *title=[[UIBarButtonItem alloc] initWithTitle:@"My Test"
  12. style:UIBarButtonItemStylePlain
  13. target:self
  14. action:nil];
  15. [toolBar setItems:[NSArray arrayWithObject:title]];
  16. [toolBar setItems:toolBarArray];
  17. [self.view addSubview:toolBar];
  18. }

然后,当运行程序的时候,加载的就是FirstViewController的视图了

最新文章

  1. .NET 环境中使用RabbitMQ(转)
  2. Data Flow ->> CDC Control Task, CDC Source, CDC Splitter
  3. C# unix时间戳转换
  4. MySQL索引原理与慢查询优化
  5. 在iframe里调用parent.func()引出的js函数运行在它们被定义的作用域里,而不是它们被执行的作用域里
  6. HDOJ(HDU) 1408 盐水的故事
  7. PAT (Advanced Level) 1062. Talent and Virtue (25)
  8. 自定义cell设置现价,原价(加横线)
  9. PHP date函数详解
  10. Hadoop学习笔记(七):初识spark
  11. javascript简单介绍
  12. GIS之家demo源代码咨询
  13. Jekyll博客添加Valine评论
  14. input文件上传(上传单个文件/多选文件/文件夹、拖拽上传、分片上传)
  15. android 开发 View _16 自定义计步器View、自定义柱状图View
  16. IE (第二部分) 浏览器 中 关于浏览器模式和文本模式
  17. 05-01 Java 方法
  18. [Umbraco] 自定义DataType中Data Editor Setting Type
  19. uitextfield 设置为密码框显示
  20. [openssl]openssl特定版本安装

热门文章

  1. Codeforces Round #358 (Div. 2) C. Alyona and the Tree dfs
  2. phpcms v9 栏目伪静态完全自定义为栏目英文目录名
  3. JavaScript权威指南--客户端存储
  4. 简述<T> 与 <?>
  5. 1-17-Linux中计划任务与日志的管理
  6. php上传文件出现500错误
  7. Nginx禁止域名恶意解析
  8. linux下使用FreeRDP 连接 Windows 远程桌面
  9. shiro权限定义的三种方法
  10. MYSQL-实现分组排序 对比 ORACLE 和SQLserver用 row_number() over(partition by ) 分组排序功能