iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)

时间:2022-09-22 18:08:51

本文转载至 http://www.tuicool.com/articles/3ymMzub

CSDN博客原文  http://blog.csdn.net/hmt20130412/article/details/34523235

本来只是打算介绍一下addChildViewController这个方法的,正好今天朋友去换工作面试问到网易新闻标签栏效果的实现,就结合它,用个小Demo实例介绍一下:(具体解释都写在了Demo里面的注释)

//
// HMTMainViewController.m
// UIScrollView
//
// Created by HMT on 14-6-25.
// Copyright (c) 2014年 humingtao. All rights reserved.
// #import "HMTMainViewController.h"
#import "HMTFirstViewController.h"
#import "HMTSecondViewController.h"
#import "HMTThirdViewController.h" @interface HMTMainViewController () <UIScrollViewDelegate> @property (nonatomic ,strong) HMTThirdViewController *thirdVC;
@property (nonatomic ,strong) HMTFirstViewController *firstVC;
@property (nonatomic ,strong) HMTSecondViewController *secondVC; @property (nonatomic ,strong) UIViewController *currentVC; @property (nonatomic ,strong) UIScrollView *headScrollView; // 顶部滚动视图 @property (nonatomic ,strong) NSArray *headArray; @end @implementation HMTMainViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view. self.navigationItem.title = @"网易新闻Demo"; self.headArray = @[@"头条",@"娱乐",@"体育",@"财经",@"科技",@"NBA",@"手机"];
/**
* automaticallyAdjustsScrollViewInsets 又被这个属性坑了
* 我"UI高级"里面一篇文章着重讲了它,大家可以去看看
*/
self.automaticallyAdjustsScrollViewInsets = NO;
self.headScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, 320, 40)];
self.headScrollView.backgroundColor = [UIColor purpleColor];
self.headScrollView.contentSize = CGSizeMake(560, 0);
self.headScrollView.bounces = NO;
self.headScrollView.pagingEnabled = YES;
[self.view addSubview:self.headScrollView];
for (int i = 0; i < [self.headArray count]; i++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(0 + i*80, 0, 80, 40);
[button setTitle:[self.headArray objectAtIndex:i] forState:UIControlStateNormal];
button.tag = i + 100;
[button addTarget:self action:@selector(didClickHeadButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.headScrollView addSubview:button]; } /*
苹果新的API增加了addChildViewController方法,并且希望我们在使用addSubview时,同时调用[self addChildViewController:child]方法将sub view对应的viewController也加到当前ViewController的管理中。
对于那些当前暂时不需要显示的subview,只通过addChildViewController把subViewController加进去;需要显示时再调用transitionFromViewController方法。将其添加进入底层的ViewController中。
这样做的好处: 1.无疑,对页面中的逻辑更加分明了。相应的View对应相应的ViewController。
2.当某个子View没有显示时,将不会被Load,减少了内存的使用。
3.当内存紧张时,没有Load的View将被首先释放,优化了程序的内存释放机制。
*/ /**
* 在iOS5中,ViewController中新添加了下面几个方法:
* addChildViewController:
* removeFromParentViewController
* transitionFromViewController:toViewController:duration:options:animations:completion:
* willMoveToParentViewController:
* didMoveToParentViewController:
*/
self.firstVC = [[HMTFirstViewController alloc] init];
[self.firstVC.view setFrame:CGRectMake(0, 104, 320, 464)];
[self addChildViewController:_firstVC]; self.secondVC = [[HMTSecondViewController alloc] init];
[self.secondVC.view setFrame:CGRectMake(0, 104, 320, 464)]; self.thirdVC = [[HMTThirdViewController alloc] init];
[self.thirdVC.view setFrame:CGRectMake(0, 104, 320, 464)]; // 默认,第一个视图(你会发现,全程就这一个用了addSubview)
[self.view addSubview:self.firstVC.view];
self.currentVC = self.firstVC; } - (void)didClickHeadButtonAction:(UIButton *)button
{
// 点击处于当前页面的按钮,直接跳出
if ((self.currentVC == self.firstVC && button.tag == 100)||(self.currentVC == self.secondVC && button.tag == 101.)) {
return;
}else{ // 展示2个,其余一样,自行补全噢
switch (button.tag) {
case 100:
[self replaceController:self.currentVC newController:self.firstVC];
break;
case 101:
[self replaceController:self.currentVC newController:self.secondVC];
break;
case 102:
//.......
break;
case 103:
//.......
break;
case 104:
//.......
break;
case 105:
//.......
break;
case 106:
//.......
break;
//.......
default:
break;
}
} } // 切换各个标签内容
- (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController
{
/**
* 着重介绍一下它
* transitionFromViewController:toViewController:duration:options:animations:completion:
* fromViewController 当前显示在父视图控制器中的子视图控制器
* toViewController 将要显示的姿势图控制器
* duration 动画时间(这个属性,old friend 了 O(∩_∩)O)
* options 动画效果(渐变,从下往上等等,具体查看API)
* animations 转换过程中得动画
* completion 转换完成
*/ [self addChildViewController:newController];
[self transitionFromViewController:oldController toViewController:newController duration:2.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) { if (finished) { [newController didMoveToParentViewController:self];
[oldController willMoveToParentViewController:nil];
[oldController removeFromParentViewController];
self.currentVC = newController; }else{ self.currentVC = oldController; }
}];
}
iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)

iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)的更多相关文章

  1. 【转】 iOS开发 剖析网易新闻标签栏视图切换&lpar;addChildViewController属性介绍&rpar;

    原文:http://blog.csdn.net/hmt20130412/article/details/34523235 本来只是打算介绍一下addChildViewController这个方法的,正 ...

  2. IOS开发--仿制网易新闻

    学习来源:袁峥老师的<快速集成App中顶部标题滚动条> 此次博文写的是按需求分析写代码,思路条理性杠杠的,可以提高的编码实现速度哦. 效果:   根据这个网易新闻的界面,需求分析:     ...

  3. iOS界面-仿网易新闻左侧抽屉式交互 续&lpar;添加新闻内容页和评论页手势&rpar;

     本文转载至  http://blog.csdn.net/totogo2010/article/details/8637430       1.介绍 有的博友看了上篇博文iOS界面-仿网易新闻左侧抽屉 ...

  4. IOS开发中UITableView(表视图)的滚动优化及自定义Cell

    IOS开发中UITableView(表视图)的滚动优化及自定义Cell IOS 开发中UITableView是非常常用的一个控件,我们平时在手机上看到的联系人列表,微信好友列表等都是通过UITable ...

  5. iOS开发app启动原理及视图和控制器的函数调用顺序

    main()函数是整个程序的入口,在程序启动之前,系统会调用exec()函数.在Unix中exec和system的不同在于,system是用shell来调用程序,相当于fork+exec+waitpi ...

  6. IOS之分析网易新闻存储数据&lpar;CoreData的使用,增删改查&rpar;

    用过网易新闻客户端的朋友们都知道,获取新闻列表时有的时候他会请求网络有时候不会,查看某条新闻的时候再返回会标注已经查看的效果,接下来分析一下是如何实现的. 首先: 1.网易新闻用CoreData存储了 ...

  7. iOS学习之网易新闻简易Demo

    简易NewsDemo代码分析 界面布局就不多说了.效果图:(自定义了三套Cell,最后一套Cell是页面最下方的"正在加载中..."Cell,图三.)         主要分析工程 ...

  8. 基于VUE&comma;VUX组件开发的网易新闻页面搭建过程

    根据妙味课堂上的一个教程练习总结,供自己复习用 一.功能介绍 一个网易新闻客户端的浏览页面,通过网易新闻的api接口实时获取新闻数据,用vux搭建样式框架,以轮播图,文字滚动,图文列表等形式把内容展示 ...

  9. iOS开发之创建颜色渐变视图View

    在iOS开发中有时需要自己自定义一个视图view的背景,而网上有人提出的在循环中不断alloc的方法设置其背景色渐变,会耗费很多内存和资源,极其不明智,而在CALayer中早就提供有图层渐变的类和相应 ...

随机推荐

  1. bs4 python解析html

    使用文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/ python的编码问题比较恶心. decode解码encode编码 在文件 ...

  2. 【Android UI】Android ListView详解

    在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示.抽空把对ListView的使用做了整理,并写了个小例子,如下图. 列表的显示需要三 ...

  3. 面向对象编程&lpar;八&rpar;——this关键字

    很多人一提起它,就说“当前对象”,可到底什么是当前对象,是什么当前对象,他自己也不清楚. this(隐式参数) 普通方法中,this总是指向当前调用该方法的对象. 构造方法中,this总是指向正要初始 ...

  4. Android 防止按钮连续点击的方法(Button&comma;ImageButton等)

    防止按钮连续点击  其实实现很简单 共通方法 public class Utils { private static long lastClickTime; public static boolean ...

  5. 解决easyui-tab添加tab滚动条问题

    //添加tab var addTab = function (title, url, icon) { if (!$('#mainTab').tabs('exists', title)) { $('#m ...

  6. CCPC L&lpar;水&rpar;

    Huatuo's Medicine Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others ...

  7. 4天html总结

  8. MQ产品比较-ActiveMQ-RocketMQ

    几种MQ产品说明: ZeroMQ :  扩展性好,开发比较灵活,采用C语言实现,实际上他只是一个socket库的重新封装,如果我们做为消息队列使用,需要开发大量的代码 RabbitMQ :结合erla ...

  9. Linux的打印rpm包的详细信息的shell脚本

    #!/bin/bash # list a content summary of a number of RPM packages # USAGE: showrpm rpmfile1 rpmfile2 ...

  10. js原生获取元素的css属性

    习惯了用jQuery的css()的方法获取元素的css属性,突然不用jquery了,当要获得元素的css时候,我瞬间停顿了一下,咦?咋获取元素的css值?比如获取元素的width.是这样么?docum ...