DZNEmptyDataSet,iOS優秀的空白頁或者出錯頁封裝

簡介

image

項目主頁:https://github.com/dzenbot/DZNEmptyDataSetios

提示:主要用於UITableView和UICollectionView,也能夠用於UIScrollView,其實主要是前兩個會用到空白或者網絡出錯頁git

採用給UIScrollView添加代理方法來給頁面添加空白頁,源碼頗有學習意義github

導入工程

自動,

pod 'DZNEmptyDataSet'

手動

https://github.com/dzenbot/DZNEmptyDataSet 下載解壓,Source文件夾下的文件拖入工程網絡

導入頭文件:

#import "UIScrollView+EmptyDataSet.h"

初始化

@interface MainViewController : UITableViewController <DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

    - (void)viewDidLoad {
         [super viewDidLoad];

         self.tableView.emptyDataSetSource = self;
         self.tableView.emptyDataSetDelegate = self;

    //這行代碼必須加上,能夠去除tableView的多餘的線,不然會影響美觀
         self.tableView.tableFooterView = [UIView new];
	}

知足代理方法,能夠分別配置,都是可選的

空白頁圖片

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"empty_placeholder"];
	}

圖片的動畫效果

- (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;
	}

標題文本,詳細描述,富文本樣式

- (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];                      
	}

按鈕文本或者背景樣式

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:17.0f]};

    return [[NSAttributedString alloc] initWithString:@"Continue" attributes:attributes];
	}
	
	- (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    return [UIImage imageNamed:@"button_image"];
	}

空白頁的背景色

- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIColor whiteColor];
	}

若是需求沒法知足,你能夠自定義

- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
	//加入你自定義的view
    UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [activityView startAnimating];
    return activityView;
	}

其餘需求

//是否顯示空白頁,默認YES
	- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return YES;
	}
	
	//是否容許點擊,默認YES
	- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return YES;
	}
	
	//是否容許滾動,默認NO
	- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
	}
	
	//圖片是否要動畫效果,默認NO
	- (BOOL) emptyDataSetShouldAllowImageViewAnimate:(UIScrollView *)scrollView {
    return YES;
	}
	
	//空白頁點擊事件
	- (void)emptyDataSetDidTapView:(UIScrollView *)scrollView {
    
	}
	
	//空白頁按鈕點擊事件
	- (void)emptyDataSetDidTapButton:(UIScrollView *)scrollView {
   
	}

注: 文章由咱們 iOS122(http://www.ios122.com)的小夥伴 @酌晨茗 整理,喜歡就一塊兒參與: iOS122 任務池app

相關文章
相關標籤/搜索