做外包很長時間挡闰,搭項(xiàng)目框架搭吐血乒融,花了半個小時搭建個基本項(xiàng)目框架,一勞永逸摄悯。
在本項(xiàng)目你能得到什么
- 基類控制器(帶刷新赞季,占位圖)
- 常用第三方收錄其中
- 工具類封裝(網(wǎng)絡(luò),管理)
- 類別擴(kuò)展奢驯,常用控件擴(kuò)展
- 常用宏定義 (常量申钩,函數(shù))
簡易總結(jié)了下,APP開發(fā)流程
屏幕快照 2017-09-18 下午2.24.51.png
BaseViewController
BaseViewController 主要對導(dǎo)航條的處理瘪阁,自定義撒遣,標(biāo)題,整體返回功能......
#import <UIKit/UIKit.h>
@interface BaseViewController : UIViewController
@property (nonatomic,strong)UINavigationBar *navigationBar;
/**
控制器title
*/
@property (nonatomic,strong)NSString *navigationTitle;
/**
設(shè)置LeftBarItem
@param title 標(biāo)題/ 非必填
*/
- (void)setLeftBarItemWithTitle:(NSString *)title;
/**
設(shè)置RightBarItem
@param icon 圖片
@param action 響應(yīng)方法
*/
- (void)setRightBarButtonItemWithIcon:(NSString *)icon Action:(SEL)action;
/**
設(shè)置RightBarItem
@param title 標(biāo)題
@param action 響應(yīng)事件
*/
- (void)setRightBarButtonItemWithTitle:(NSString *)title Action:(SEL)action;
/**
設(shè)置RightBarItems
@param icon1 圖標(biāo)1
@param action1 事件1
@param icon2 圖片2
@param action2 事件2
*/
- (void)setRightBarButtonItemWithIcon1:(NSString *)icon1 Action1:(SEL)action1 AndIcon2:(NSString *)icon2 Action:(SEL)action2;
@end
#import "BaseViewController.h"
#import "UIButton+Extension.h"
@interface BaseViewController ()
/**
注意:整體返回思路->
1,隱藏系統(tǒng)的NavigationBar;
2,自定義navigationBar 添加在Controller.View 上 坐標(biāo)是:(0,0,屏幕寬,64)
3,在表視圖中(繼承與BaseCollectionViewController管跺,BaseTableViewController)的控制器中义黎,表視圖的坐標(biāo)(0,0豁跑,屏幕寬廉涕,屏幕高),通過修改 contentInset屬性調(diào)整艇拍;
4狐蜕,對于不是表視圖控制器,其子控件坐標(biāo)從(0卸夕,64)開始計(jì)算
*/
@property (nonatomic,strong)UINavigationItem *navItem;
@property (nonatomic,strong)UILabel *titleLabel;
@end
@implementation BaseViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
self.edgesForExtendedLayout = UIRectEdgeBottom;
[self createNavigationBar];
}
#pragma mark - 導(dǎo)航條
- (void)createNavigationBar
{
// 創(chuàng)建一個導(dǎo)航欄
self.navigationBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 64)];
// 導(dǎo)航欄背景顏色
self.navigationBar.barTintColor = [UIColor returnColorWithHexString:@"#FFFFFF"];
// 創(chuàng)建導(dǎo)航欄組件
self.navItem =[[UINavigationItem alloc]init];
// 自定義導(dǎo)航欄的title馏鹤,用UILabel實(shí)現(xiàn)
_titleLabel = [UILabel getLabelWithFontSize:18 AndTextColer:@"#333333" AndBackGroundColor:nil];
_titleLabel.frame = CGRectMake(0, 0, 100, 44);
_titleLabel.textAlignment = NSTextAlignmentCenter;
self.navItem.titleView = _titleLabel;
[self.navigationBar pushNavigationItem:self.navItem animated:false];
[self.view addSubview:self.navigationBar];
}
#pragma mark - 標(biāo)題
- (void)setNavigationTitle:(NSString *)navigationTitle
{
_navigationTitle = navigationTitle;
self.titleLabel.text = navigationTitle;
}
#pragma mark - LeftBarItem
- (void)setLeftBarItemWithTitle:(NSString *)title
{
UIButton *button = [UIButton getButtonWithFontSize:15 AndTextColor:@"#666666" AndBackGroundColor:nil];
[button setButtonNormalImage:@"leftarrow" SelectImage:nil];
[button setTitle:title forState:UIControlStateNormal];
button.contentHorizontalAlignment = NSTextAlignmentRight;
[button addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
CGFloat titleWith = StringWidth(title, 18, 18);
if (title == nil || title.length == 0) {
button.frame = CGRectMake(0, 0, 44, 44);
button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 44 - 8);
}else{
button.frame = CGRectMake(0, 0, titleWith + 10, 44);
button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, titleWith - 8);
}
// 調(diào)整LeftButton 邊距的問題
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -5;
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithCustomView:button];
self.navItem.leftBarButtonItems = @[negativeSpacer,leftBarButton];
}
#pragma mark - RightBarItem
- (void)setRightBarButtonItemWithIcon:(NSString *)icon Action:(SEL)action
{
if (icon != nil || icon.length != 0) {
[self setRightBarItemWithImage:icon title:nil action:action];
}
}
- (void)setRightBarButtonItemWithTitle:(NSString *)title Action:(SEL)action
{
if (title != nil || title.length != 0) {
[self setRightBarItemWithImage:nil title:title action:action];
}
}
- (void)setRightBarItemWithImage:(NSString *)icom title:(NSString *)title action:(SEL)action
{
UIButton *button = [UIButton getButtonWithFontSize:15 AndTextColor:@"#666666" AndBackGroundColor:nil];
[button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
if (icom == nil || icom.length == 0) {
CGFloat titleWith = StringWidth(title, 18, 18);
button.frame = CGRectMake(0, 0, titleWith, 44);
[button setTitle:title forState:UIControlStateNormal];
}else{
button.frame = CGRectMake(0, 0, 44, 44);
[button setImage:[UIImage imageNamed:icom] forState:UIControlStateNormal];
}
// 調(diào)整LeftButton 邊距的問題
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -5;
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithCustomView:button];
self.navItem.rightBarButtonItems = @[negativeSpacer,rightBarButton];
}
- (void)setRightBarButtonItemWithIcon1:(NSString *)icon1 Action1:(SEL)action1 AndIcon2:(NSString *)icon2 Action:(SEL)action2
{
UIBarButtonItem *rightBarButton1 = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:icon1] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:action1];
UIBarButtonItem *rightBarButton2 = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:icon2] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:action2];
self.navItem.rightBarButtonItems = @[rightBarButton1,rightBarButton2];
}
- (void)backAction
{
[self.navigationController popViewControllerAnimated:YES];
}
@end
BaseTableViewController
主要對最常用的TableViewController處理,集成刷新娇哆,占位圖湃累。。碍讨。
#import "BaseViewController.h"
@interface BaseTableViewController : BaseViewController
@property (nonatomic,strong)UITableView *mainTableView;// 表格
@property (nonatomic,strong)NSMutableArray *dataArray;// 數(shù)據(jù)源
@property (nonatomic,assign)NSInteger pageCount;// 頁數(shù)
@property (nonatomic,assign)NSInteger number;//每頁條數(shù)
#pragma mark - 上拉加載更多治力,下拉刷新
/**
去除上下拉加載
*/
- (void)removedRefreshing;
/**
隱藏加載更多
*/
- (void)hideLoadMoreRefreshing;
/**
結(jié)束刷新
*/
- (void)endRefreshing;
/**
刷新表格
*/
- (void)refreshData;
@end
#import "BaseTableViewController.h"
#import "UIScrollView+EmptyDataSet.h"
#import "MJRefresh.h"
@interface BaseTableViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (nonatomic, getter=isLoading)BOOL loading;
@end
@implementation BaseTableViewController
- (void)setLoading:(BOOL)loading
{
if (self.loading == loading) {
return;
}
_loading = loading;
[self.mainTableView reloadEmptyDataSet];
}
- (NSMutableArray *)dataArray
{
if (_dataArray == nil) {
_dataArray = [[NSMutableArray alloc]init];
}
return _dataArray;
}
#pragma mark - 懶加載
- (UITableView *)mainTableView
{
if (!_mainTableView) {
_mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, K_Screen_width, K_Screen_height) style:UITableViewStyleGrouped];
_mainTableView.delegate = self;
_mainTableView.dataSource = self;
_mainTableView.scrollsToTop = YES;
_mainTableView.emptyDataSetSource = self;
_mainTableView.emptyDataSetDelegate = self;
_mainTableView.showsVerticalScrollIndicator = NO;
_mainTableView.showsHorizontalScrollIndicator = NO;
_mainTableView.backgroundColor = [UIColor whiteColor];
_mainTableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
_mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(reloadNewData)];
_mainTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(reloadMoreData)];
}
return _mainTableView;
}
- (void)viewDidLoad {
[super viewDidLoad];
_pageCount = 1;
_number = 10;
self.dataArray = @[@""].mutableCopy;// 防止剛進(jìn)入頁面時顯示出占位圖
[self.view insertSubview:self.mainTableView belowSubview:self.navigationBar];
[self requestData];
}
#pragma mark - 網(wǎng)絡(luò)請求 (子類重寫)
- (void)requestData
{
[self endRefreshing];
}
#pragma mark - 上下拉獲取數(shù)據(jù)
- (void)reloadNewData
{
_pageCount = 1;
[self requestData];
}
- (void)reloadMoreData
{
_pageCount ++;
[self requestData];
}
- (void)removedRefreshing
{
self.mainTableView.mj_header = nil;
self.mainTableView.mj_footer = nil;
}
- (void)endRefreshing
{
[self.mainTableView.mj_header endRefreshing];
[self.mainTableView.mj_footer endRefreshing];
}
- (void)hideLoadMoreRefreshing
{
self.mainTableView.mj_footer.hidden = YES;
}
- (void)refreshData
{
[self.mainTableView reloadData];
}
#pragma mark - TableViewDelegate dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.0001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0.0001;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cell_id = @"falg";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_id];
}
cell.textLabel.text = [NSString stringWithFormat:@"%ld分區(qū)%ld行",indexPath.section,indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
#pragma mark - DZNEmptyDataSetSource
/**
* 返回文字詳情
*/
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
{
NSString *text = @"點(diǎn)擊圖片重新加載";
NSMutableAttributedString *attribuString = [[NSMutableAttributedString alloc]initWithString:text attributes:nil];
[attribuString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PF-SC-Regular" size:12] range:[attribuString.string rangeOfString:@"哈哈哈"]];
return attribuString;
}
/**
* 調(diào)整垂直位置
*/
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
return [UIImage imageNamed:@"zhanwei"];
}
//返回loading的狀態(tài)
- (BOOL)emptyDataSetShouldAnimateImageView:(UIScrollView *)scrollView
{
return self.isLoading;
}
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
return -64.f;
}
#pragma mark - DZNEmptyDataSetDelegate
/**
* 空白區(qū)域點(diǎn)擊事件
*/
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view
{
/*
動畫效果
self.loading = YES;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.loading = NO;
});
[self requestData];
*/
// 刷新
[self.mainTableView.mj_header beginRefreshing];
}
- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
animation.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0) ];
animation.duration = 0.25;
animation.cumulative = YES;
animation.repeatCount = MAXFLOAT;
return animation;
}
@end
具體看demo,就不一一舉栗子了。