github下载地址
https://github.com/dzenbot/DZNEmptyDataSet
提示
DZNEmptyDataSet主要用于UITableView和UICollectionView,也可以用于UIScrollView,其实主要是前两个会用到空白或者网络出错页
采用给UIScrollView添加代理方法来给页面添加空白页,源码很有学习意义
集成步骤:
01.下载解压后,将Source文件夹下的文件(如下图)拖入工程
02.在项目工程中导入头文件,并遵守数据源和代理方法
#import "UIScrollView+EmptyDataSet.h"
<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
03.初始化
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.emptyDataSetSource = self;
self.tableView.emptyDataSetDelegate = self;
// 加上这行代码后,可以去除tableView多余的线
self.tableView.tableFooterView = [UIView new];
}
04.简单列举一些代理方法,其他方法请自行下载查看
// 空白页图片
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
return [UIImage imageNamed:@"图片名称"];
}
// 图片的动画效果(默认为关闭,需要调用代理方法emptyDataSetShouldAnimateImageView进行开启)
- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: @"transform"];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 0.0, 1.0, 0.0)];
animation.duration = 1.0;
animation.cumulative = YES;
animation.repeatCount = MAXFLOAT;
return animation;
}
// 标题文本,富文本样式
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
NSString *text = @"Please Allow Photo Access";
NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:18.0f],
NSForegroundColorAttributeName: [UIColor darkGrayColor]};
return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
// 标题文本下面的详细描述,富文本样式
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
NSString *text = @"This allows you to share photos from your library and save photos to your camera roll.";
NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
paragraph.lineBreakMode = NSLineBreakByWordWrapping;
paragraph.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f],
NSForegroundColorAttributeName: [UIColor lightGrayColor],
NSParagraphStyleAttributeName: paragraph};
return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
// 空白页的背景色
- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
return [UIColor whiteColor];
}
// 如果需求无法满足,也可以自定义需要显示的view
// 感谢评论的几位朋友提出的问题:关于自定义view,先创建自定义view后,再在此view上添加所要展示的imageView或其他控件即可正常显示,单纯的创建一个view是无法显示的
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
//加入你自定义的view
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[activityView startAnimating];
return activityView;
}
// 图片与标题文本,以及标题文本和详细描述之间的垂直距离,默认是11pts
- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView{
return 11;
}
// 是否 显示空白页,默认是YES
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView{
return YES;
}
// 是否 允许图片有动画效果,默认NO(设置为YES后,动画效果才会有效)
- (BOOL)emptyDataSetShouldAnimateImageView:(UIScrollView *)scrollView{
return YES;
}
// 是否 允许上下滚动,默认NO
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
return YES;
}
// 是否 允许点击,默认是YES
- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView{
return YES;
}
// 点击中间的图片和文字时调用
- (void)emptyDataSetDidTapView:(UIScrollView *)scrollView{
NSLog(@"点击了view");
}
转自iOS122,略有修改