项目搭建
一、设计模式
首先呢,小Q采用传统的MVC的设计模式,优点我们再来啰嗦一下啊:1、多个视图可以对应一个模型。按MVC设计模式,一个模型对应多个视图,可以减少代码的复制及代码的维护量,一旦模型发生改变,也易于维护。2、应用被分隔为三层,降低了各层之间的耦合,提供了应用的可扩展性。3、控制层的概念也很有效,由于它把不同的模型和不同的视图组合在一起,完成不同的请求。因此,控制层可以说是包含了用户请求权限的概念。4、MVC更符合软件工程化管理的精神。不同的层各司其职,每一层的组件具有相同的特征,有利于通过工程化和工具化产生管理程序代码。转换为个人的理解就是MVC各做个的事情,把自己的工作负责好,由C来控制MV的交互,出问题了好解决,能快速找出问题点,解耦合项目目录路径如下:
好进行下一步,添加PCH文件
二、添加pch文件pch的作用:1.存放一些全局的宏(整个项目中都用得上的宏)2.用来包含一些全部的头文件(整个项目中都用得上的头文件)3.能自动打开或者关闭日志输出功能但是apple在Xcode 6中去掉了pch,为了一些琐碎的头文件引用,加快了 编译速度!习惯了pch的小伙伴们很不适应,比如我,添加方法如下:(1)创建command+n ----> PCH File(2)配置,在工程的TARGETS里边Building Setting中搜索Prefix Header,然后把Precompile Prefix Header右边的NO改为Yes、在Precompile Prefix Header下边的Prefix Header右边双击,添加刚刚创建的pch文件的工程路径,添加格式:“$(SRCROOT)/项目名称/pch文件名”
可能出现问题:
在pch文件中添加常用的宏 如下:
//16进制颜色#define UIColorFromRGB(rgbValue) [UIColor \colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \ green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \ blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0] //屏幕高度 #define SCREEN_HEIGHT [UIScreen mainScreen ].bounds.size.height //屏幕宽度 #define SCREEN_WIDTH [UIScreen mainScreen ].bounds.size.width //获取通知中心 #define LRNotificationCenter [NSNotificationCenter defaultCenter] //弱引用 #define WeakSelf(type) __weak typeof(type) weak##type = type; //强引用 #define StrongSelf(type) __strong typeof(type) type = weak##type; //GCD - 一次性执行 #define kDISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock); //GCD - 在Main线程上运行 #define kDISPATCH_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock); //GCD - 开启异步线程 #define kDISPATCH_GLOBAL_QUEUE_DEFAULT(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlocl);
三、cocoapods 使用
cocoapods这个大家都不陌生,怎么装怎么用都不讲了(不会的自行百度)啰嗦一点是用淘宝的Ruby镜像来访问cocoapods 这个已经不适用了,我发现了一个新的这个是可以用的,目前我用的这个
使用pod 必须要由一个podfile文件,0.X的版本和1.X的版本文件有所不一样,每次都创建一个很麻烦,一般都是拷贝一个之前有的文件全选后替换如下代码,然后install 就可以了
platform :ios, "8.1"target '项目名字' doend
小Q会动的tabbar
什么是会动的tabbar呢,先看一个gif就知道了
pod 'pop'
然后在终端中
pod install
在需要动画的地方加入如下代码:
POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewScaleXY]; scaleAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(1, 1)]; scaleAnimation.velocity = [NSValue valueWithCGPoint:CGPointMake(10, 10)]; scaleAnimation.springBounciness = 60.f; scaleAnimation.delegate = self; [(需要动画的View) pop_addAnimation:scaleAnimation forKey:@"scaleAnimation"];
整个TabBarViewController的代码如下,很简单的东西就不做过多的讲解了:
//// GD_TabBarViewController.m// GD_XiaoQ//// Created by GuangdongQi on 2016/12/20. // Copyright © 2016年 GuangdongQi. All rights reserved. // #import "GD_TabBarViewController.h" #import#import "GD_CalendarViewController.h" #import "GD_WeatherViewController.h" #import "GD_XiaoQViewController.h" #import "GD_BaseNavigationController.h" @interface GD_TabBarViewController () @property (nonatomic, strong) NSMutableArray *buttons; @property (nonatomic, strong) UIView *tabbarview; @property (nonatomic, assign) NSInteger btnTag; @end @implementation GD_TabBarViewController - (void)viewDidLoad { [super viewDidLoad]; //修改tabbar上面线条的颜色 self.tabBar.layer.borderWidth = 0.50; self.tabBar.layer.borderColor = UIColorFromRGB(0xdddddd).CGColor; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tabbarIndexChange:) name:@"Notification_Tabbar" object:nil]; GD_CalendarViewController * calendarVC = [[GD_CalendarViewController alloc]init]; calendarVC.tabBarItem = nil; GD_WeatherViewController * weatherVC = [[GD_WeatherViewController alloc]init]; weatherVC.tabBarItem = nil; GD_XiaoQViewController * xiaoQVC = [[GD_XiaoQViewController alloc]init]; xiaoQVC.tabBarItem = nil; NSMutableDictionary *imgDic1 = [NSMutableDictionary dictionaryWithCapacity:3]; [imgDic1 setObject:[UIImage imageNamed:@"icon_tabbar_00_n"] forKey:@"Default"]; [imgDic1 setObject:[UIImage imageNamed:@"icon_tabbar_00_s"] forKey:@"Highlighted"]; [imgDic1 setObject:[UIImage imageNamed:@"icon_tabbar_00_s"] forKey:@"Seleted"]; NSMutableDictionary *imgDic2 = [NSMutableDictionary dictionaryWithCapacity:3]; [imgDic2 setObject:[UIImage imageNamed:@"icon_tabbar_01_n"] forKey:@"Default"]; [imgDic2 setObject:[UIImage imageNamed:@"icon_tabbar_01_s"] forKey:@"Highlighted"]; [imgDic2 setObject:[UIImage imageNamed:@"icon_tabbar_01_s"] forKey:@"Seleted"]; NSMutableDictionary *imgDic3 = [NSMutableDictionary dictionaryWithCapacity:3]; [imgDic3 setObject:[UIImage imageNamed:@"icon_tabbar_02_n"] forKey:@"Default"]; [imgDic3 setObject:[UIImage imageNamed:@"icon_tabbar_02_s"] forKey:@"Highlighted"]; [imgDic3 setObject:[UIImage imageNamed:@"icon_tabbar_02_s"] forKey:@"Seleted"]; NSArray *vcs = @[calendarVC,weatherVC,xiaoQVC]; NSMutableArray *navs = [NSMutableArray arrayWithCapacity:[vcs count]]; for (UIViewController *vc in vcs) { GD_BaseNavigationController *BaseNavigation = [[GD_BaseNavigationController alloc] initWithRootViewController:vc]; BaseNavigation.hidesBottomBarWhenPushed = NO; [navs addObject:BaseNavigation]; } [self setViewControllers:navs]; [self setImages:@[imgDic1/*,imgDic1*/,imgDic2,/*imgDic3,*/imgDic3]]; [self setSelectedIndex:0]; } /** * 设置tab项图片 * * imgs */ -(void)setImages:(NSArray*)imgs { self.tabbarview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 49)]; self.tabbarview.backgroundColor = UIColorFromRGB(0xfdfcfc); for(UIView *view in self.view.subviews) { if([view isKindOfClass:[UITabBar class]]) { [view addSubview:self.tabbarview]; [view bringSubviewToFront:self.tabbarview]; break; } } self.buttons = [NSMutableArray arrayWithCapacity:[imgs count]]; CGFloat width = SCREEN_WIDTH / [imgs count]; for (int i = 0; i < [imgs count]; i++) { UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.tag = i; btn.frame = CGRectMake(width * i, 0, width, self.tabbarview.frame.size.height); [btn setImage:[[imgs objectAtIndex:i] objectForKey:@"Default"] forState:UIControlStateNormal]; [btn setImage:[[imgs objectAtIndex:i] objectForKey:@"Highlighted"] forState:UIControlStateHighlighted]; [btn setImage:[[imgs objectAtIndex:i] objectForKey:@"Seleted"] forState:UIControlStateSelected]; [btn addTarget:self action:@selector(touchButton:) forControlEvents:UIControlEventTouchUpInside]; [self.buttons addObject:btn]; [self.tabbarview addSubview:btn]; } [self selectTabAtIndex:0]; } /** * 设置当前选择tab * * index */ - (void)selectTabAtIndex:(NSInteger)index { NSLog(@"选择 %ld",(long)index); self.selectedIndex = index; for (int i = 0; i < [self.buttons count]; i++) { UIButton *b = [self.buttons objectAtIndex:i]; b.selected = NO; b.userInteractionEnabled = YES; } UIButton *btn = [self.buttons objectAtIndex:index]; btn.selected = YES; btn.userInteractionEnabled = NO; } -(void) touchButton:(id)sender { UIButton *btn = sender; self.btnTag = btn.tag; POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewScaleXY]; scaleAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(1, 1)]; scaleAnimation.velocity = [NSValue valueWithCGPoint:CGPointMake(10, 10)]; scaleAnimation.springBounciness = 60.f; scaleAnimation.delegate = self; [btn pop_addAnimation:scaleAnimation forKey:@"scaleAnimation"]; } -(void)pop_animationDidStart:(POPAnimation *)anim{ [self selectTabAtIndex:self.btnTag]; } - (void) tabbarIndexChange:(NSNotification *)notification { NSInteger selectTabAtIndex = [notification.object[@"index"] integerValue]; [self selectTabAtIndex:selectTabAtIndex]; } @end