你的位置:首页 > 操作系统

[操作系统]IOS 项目实战(百思不得姐-1)


今天开始正式百思不得姐项目的实战,在未来的一段时间有可能因为其他原因不能正常更新,但是会尽量克服。

首先带来的是今天的效果图->

 

今天实现主要是框架以及一些优化。今天的重点算是优化吧(优化的内容主要是减少了代码量,时代码的可读性上升)

一 PrefixHeader 的配置

代码->

//// PrefixHeader.pch// 百思不得姐//// Created by ma c on 16/3/23.// Copyright © 2016年 xubaoaichiyu. All rights reserved.//#ifndef _1_______PrefixHeader_pch#define _1_______PrefixHeader_pch#import "UIView+CXExtension.h"#import "UIBarButtonItem+CXExtension.h"#ifdef DEBUG#define CXLog(...) NSLog(__VA_ARGS__)#else#define CXLog(...)#endif#define CXLogFunc CXLog(@"%s", __func__)#define CXRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]#define CXGlobalBg CXRGBColor(223, 223, 223)#endif

二 UIView的类别(为了更方便的对继承自UIView的控件进行操作)

代码- >

//// UIView+CXExtension.h// 百思不得姐//// Created by ma c on 16/3/23.// Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import <UIKit/UIKit.h>@interface UIView (CXExtension)@property (nonatomic, assign) CGSize size;@property (nonatomic, assign) CGFloat width;@property (nonatomic, assign) CGFloat height;@property (nonatomic, assign) CGFloat x;@property (nonatomic, assign) CGFloat y;@end

//// UIView+CXExtension.m// 百思不得姐//// Created by ma c on 16/3/23.// Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "UIView+CXExtension.h"@implementation UIView (CXExtension)- (void)setSize:(CGSize)size{  CGRect frame = self.frame;  frame.size = size;  self.frame = frame;}- (CGSize)size{  return self.frame.size;}- (void)setWidth:(CGFloat)width{  CGRect frame = self.frame;  frame.size.width = width;  self.frame = frame;}- (void)setHeight:(CGFloat)height{  CGRect frame = self.frame;  frame.size.height = height;  self.frame = frame;}- (void)setX:(CGFloat)x{  CGRect frame = self.frame;  frame.origin.x = x;  self.frame = frame;}- (void)setY:(CGFloat)y{  CGRect frame = self.frame;  frame.origin.y = y;  self.frame = frame;}- (CGFloat)width{  return self.frame.size.width;}- (CGFloat)height{  return self.frame.size.height;}- (CGFloat)x{  return self.frame.origin.x;}- (CGFloat)y{  return self.frame.origin.y;}@end

三 UITabBar自定义(自定义的好处大家都知道)

代码- >

//// CXTabBar.m// 百思不得姐//// Created by ma c on 16/3/23.// Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "CXTabBar.h"@interface CXTabBar()/** 发布按钮 */@property (nonatomic, weak) UIButton *publishButton;@end@implementation CXTabBar- (instancetype)initWithFrame:(CGRect)frame{  if (self = [super initWithFrame:frame]) {    // 设置tabbar的背景图片    [self setBackgroundImage:[UIImage imageNamed:@"tabbar-light"]];        // 添加发布按钮    UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];    // 设置发布按钮普通状态下的背景图片    [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];    // 设置发布按钮高亮状态下的背景图片    [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];    // 设置发布按钮的大小等于当前背景图片的大小    publishButton.size = publishButton.currentBackgroundImage.size;    [self addSubview:publishButton];    self.publishButton = publishButton;  }  return self;}- (void)layoutSubviews{  [super layoutSubviews];    CGFloat width = self.width;  CGFloat height = self.height;    // 设置发布按钮的frame  self.publishButton.center = CGPointMake(width * 0.5, height * 0.5);    // 设置其他UITabBarButton的frame  CGFloat buttonY = 0;  CGFloat buttonW = width / 5;  CGFloat buttonH = height;  NSInteger index = 0;  for (UIView *button in self.subviews) {    if (![button isKindOfClass:[UIControl class]] || button == self.publishButton) continue;        // 计算按钮的x值    CGFloat buttonX = buttonW * ((index > 1)?(index + 1):index);    button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);        // 增加索引    index++;  }}@end

四 CXTabBarController

代码->

//// CXTabBarController.m// 百思不得姐//// Created by ma c on 16/3/23.// Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "CXTabBarController.h"#import "CXTabBar.h"#import "CXNavigationController.h"#import "CXEssenceViewController.h"#import "CXFriendTrendsViewController.h"#import "CXMeViewController.h"#import "CXNewViewController.h"@interface CXTabBarController ()@end@implementation CXTabBarController//只会调用一次。具体意义请看之前随笔+(void)initialize{    // 通过appearance统一设置所有UITabBarItem的文字属性  // 后面带有UI_APPEARANCE_SELECTOR的方法, 都可以通过appearance对象来统一设置  NSDictionary * attrs = @{                             NSFontAttributeName:[UIFont systemFontOfSize:12],               NSForegroundColorAttributeName:[UIColor grayColor]                             };    NSDictionary * selectAttrs = @{                                   NSFontAttributeName:[UIFont systemFontOfSize:12],                  NSForegroundColorAttributeName:[UIColor darkGrayColor]                                   };    UITabBarItem * item = [UITabBarItem appearance];  [item setTitleTextAttributes:attrs forState:UIControlStateNormal];  [item setTitleTextAttributes:selectAttrs forState:UIControlStateSelected];  }- (void)viewDidLoad{  [super viewDidLoad];    // 添加子控制器  [self setupChildVc:[[CXEssenceViewController alloc] init] title:@"精华" image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon"];    [self setupChildVc:[[CXNewViewController alloc] init] title:@"新帖" image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon"];    [self setupChildVc:[[CXFriendTrendsViewController alloc] init] title:@"关注" image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_friendTrends_click_icon"];    [self setupChildVc:[[CXMeViewController alloc] init] title:@"我" image:@"tabBar_me_icon" selectedImage:@"tabBar_me_click_icon"];    // 更换tabBar(KVC)  [self setValue:[[CXTabBar alloc] init] forKeyPath:@"tabBar"];}/** * 初始化子控制器 */- (void)setupChildVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage{  // 设置文字和图片  vc.navigationItem.title = title;  vc.tabBarItem.title = title;  vc.tabBarItem.image = [UIImage imageNamed:image];  vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];    // 包装一个导航控制器, 添加导航控制器为tabbarcontroller的子控制器  CXNavigationController *nav = [[CXNavigationController alloc] initWithRootViewController:vc];  [self addChildViewController:nav];}@end

(一些没有用到的文件没有说明,会在以后用到的时候说明)

五 框架展示

 

今天是正式写百思不得姐的第一天,之所倚拖了几天是因为这几天没有时间,我会尽量在较短的时间内把百思不得姐写完,如果在项目中有什么不足的地方也希望大家指正,共同进步。