寒假临近,介于现在时间的充裕,本人准备开始新起高仿一个大家最为熟悉也最为娱乐的一个社交平台,新浪微博.本期实战一是对自己以前学习的知识复习,也是进行笔记整理,如有喜欢的朋友请star.也希望也能热爱喜欢开发的小白同学一点帮助.感谢大家.
-
本次项目,所用的代码管理工具为Git.带托管的管理平台为Github.如有不知道怎么上传到github的同学,请参考我的以前文章是描述怎么上传项目的.请戳这里http://www.jianshu.com/p/c1c604ff0777
-
本次项目是采取章节制,对于每天的项目都是按照项目流程的方法进行开发,会一步一步的进行,代码的整体就跟笔记的整体有关,思想上的连贯这才是最重要的,每次项目都会发一张GIF图来大概介绍今天所做的一些内容.
-
本次所高仿的项目是新浪微博,由于平台比较大.资源方法也比较宽广,有关抓包,图片,API数据接口,这些都不是问题.有关图片的抓包,大家可以利用ituns进行获取.详情前往http://www.jianshu.com/p/6ca768c5d16c
-
由于项目的深入,本文的内容也是又浅到深,代码这块也是从易到难(大神误入),相信前期的内容可能都是大家熟悉的内容,到了最后会一步一步的进行完善.
-
GIF 展示
第一天内容gif介绍
项目介绍
- 由于本次项目是摈弃掉故事版都是用到的纯代码开发,这样可以达到需要训练的目的,今天微博的第一天就是开始进行整个项目架构的搭建跟展示,没有多大的难度.
![](https://img.haomeiwen.com/i1170421/b3a050919165ec6a.png)
- 本次项目是使用传统的MVC思想进行开发,一共需要的暂时是<首页><消息><发现><我>加上自己的主要的控制器进行开发,后续有需求会继续添加新的控制器页面.
代码如下展示:
- 设置主窗口
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
WKTabBarController *tabBarVc = [[WKTabBarController alloc]init];
self.window.rootViewController = tabBarVc;
[self.window makeKeyAndVisible];
return YES;
}
- 自定义TabBar控制器用来添加四个不同的子控制器
- 创建子控制器
- (void)setUpOneVc {
WKHomeTableViewController *homeVc = [[WKHomeTableViewController alloc]init];
[self addOneChildVc:homeVc title:@"首页" image:@"tabbar_home" selectImage:@"tabbar_home_selected"];
WKMessageTableViewController *messageVc = [[WKMessageTableViewController alloc]init];
[self addOneChildVc:messageVc title:@"消息" image:@"tabbar_message_center" selectImage:@"tabbar_message_center_selected"];
WKDiscoveryTableViewController *disVc = [[WKDiscoveryTableViewController alloc]init];
[self addOneChildVc:disVc title:@"发现" image:@"tabbar_discover" selectImage:@"tabbar_discover_selected"];
WKMeTableViewController *meVc = [[WKMeTableViewController alloc]init];
[self addOneChildVc:meVc title:@"我" image:@"tabbar_profile" selectImage:@"tabbar_profile_selected"];
}
- (void)addOneChildVc: (UIViewController *)childVc title: (NSString *)title image: (NSString *)image selectImage: (NSString *)selectImage{
childVc.tabBarItem.title = title;
childVc.navigationItem.title = title;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSForegroundColorAttributeName] = [UIColor orangeColor];
[childVc.tabBarItem setTitleTextAttributes:dict forState:UIControlStateSelected];
childVc.tabBarItem.image = [UIImage imageNamed:image];
UIImage *selectedImage = [UIImage imageNamed:selectImage];
selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
childVc.tabBarItem.selectedImage = selectedImage;
WKNavigationController *navVc = [[WKNavigationController alloc]initWithRootViewController:childVc];
[self addChildViewController:navVc];
}
- 根据每次导航栏上的按钮是重复相同的代码,可以抽出来进行封装一个分类.
@interface UIBarButtonItem (Extension)
+ (UIBarButtonItem *)itemWithImage: (NSString *)image HightImage: (NSString *)hightImage target:(id)target action:(SEL)action;
+ (UIBarButtonItem *)itemWithImage: (NSString *)image HightImage: (NSString *)hightImage target:(id)target action:(SEL)action {
UIButton *btn = [[UIButton alloc]init];
[btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:hightImage] forState:UIControlStateHighlighted];
btn.size = btn.currentBackgroundImage.size;
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return [[UIBarButtonItem alloc]initWithCustomView:btn];
}
- 对于自定义的导航栏每次进入跳转到不同的控制器去都需要隐藏底部的tabbar条,可以直接在导航栏的push方法中进行拦截.可以让我们达到事半功倍的效果,还有就是在进入诸多的控制器的时候会有返回的箭头,这个也是经常使用到的,这个可以进行push操作中进行拦截
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (self.viewControllers.count > 0) {
self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationbar_back" HightImage:@"navigationbar_back_highlighted" target:self action:@selector(back)];
self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationbar_more" HightImage:@"navigationbar_more_highlighted" target:self action:@selector(more)];
viewController.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:animated];
}
- 今天最后的一个知识点也就是对于导航栏上的buttonitem 这种属性的文字大小,颜色大小的同意设置.这个其实可以用到外观对象,对于操控全局的设置对象,都可以进行一致性的方法操作
/**
这个方法的作用是在加载这个类的时候只调用一次
*/
+ (void)initialize {
//设置外观对象
UIBarButtonItem *item = [UIBarButtonItem appearance];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSForegroundColorAttributeName] = [UIColor orangeColor];
dict[NSFontAttributeName] = [UIFont systemFontOfSize:14];
[item setTitleTextAttributes:dict forState:UIControlStateNormal];
NSMutableDictionary *hightDict = [NSMutableDictionary dictionary];
hightDict[NSForegroundColorAttributeName] = [UIColor redColor];
hightDict[NSFontAttributeName] = [UIFont systemFontOfSize:14];
[item setTitleTextAttributes:hightDict forState:UIControlStateHighlighted];
}
- 今天内容不是很多,可以回顾下自己以前的知识.感谢.
网友评论