Principle of blank page background and principle of page background prompt

Source: Internet
Author: User
Tags blank page

Principle of blank page background and principle of page background prompt

When there is no data in our list or an error occurs in the request network, the view background will be prompted accordingly. For example, the following effect:

Here is the effect of combining an image with a text. You can view the source code of the Codin.net project in the source code;

1: First encapsulate the background into a view

@interface EaseBlankPageView : UIView@property (strong, nonatomic) UIImageView *monkeyView;@property (strong, nonatomic) UILabel *tipLabel;@property (strong, nonatomic) UIButton *reloadButton;@property (copy, nonatomic) void(^reloadButtonBlock)(id sender);- (void)configWithType:(EaseBlankPageType)blankPageType hasData:(BOOL)hasData hasError:(BOOL)hasError reloadButtonBlock:(void(^)(id sender))block;@end
@ Implementation EaseBlankPageView-(instancetype) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {self. backgroundColor = [UIColor clearColor];} return self;}-(void) configWithType :( EaseBlankPageType) blankPageType hasData :( BOOL) hasData hasError :( BOOL) hasError reloadButtonBlock :( void (^) (id) block {if (hasData) {[self removeFromSuperview]; return;} self. alpha = 1.0; // figure Slice if (! _ MonkeyView) {_ monkeyView = [[UIImageView alloc] initWithFrame: CGRectZero]; [self addSubview: _ monkeyView];} // text if (! _ TipLabel) {_ tipLabel = [[UILabel alloc] initWithFrame: CGRectZero]; _ tipLabel. backgroundColor = [UIColor clearColor]; _ tipLabel. numberOfLines = 0; _ tipLabel. font = [UIFont systemFontOfSize: 17]; _ tipLabel. textColor = [UIColor lightGrayColor]; _ tipLabel. textAlignment = NSTextAlignmentCenter; [self addSubview: _ tipLabel];} // layout [_ monkeyView mas_makeConstraints: ^ (MASConstraintMaker * make) {make. cent ErX. similar to (self); make. bottom. similar to (self. mas_centerY) ;}]; [_ tipLabel mas_makeConstraints: ^ (MASConstraintMaker * make) {make. left. right. centerX. similar to (self); make. top. similar to (_ monkeyView. mas_bottom); make. height. mas_failed to (50) ;}]; _ reloadButtonBlock = nil; if (hasError) {// loading failed if (! _ ReloadButton) {_ reloadButton = [[UIButton alloc] initWithFrame: CGRectZero]; [_ reloadButton setImage: [UIImage imageNamed: @ "success"] forState: UIControlStateNormal]; _ reloadButton. response = YES; [_ reloadButton addTarget: self action: @ selector (reloadButtonClicked :) forControlEvents: UIControlEventTouchUpInside]; [self addSubview: _ reloadButton]; [_ reloadButton mas_ma KeConstraints: ^ (MASConstraintMaker * make) {make. centerX. similar to (self); make. top. similar to (_ tipLabel. mas_bottom); make. size. mas_resourceto (CGSizeMake (160, 60) ;}] ;}_ reloadButton. hidden = NO; _ reloadButtonBlock = block; [_ monkeyView setImage: [UIImage imageNamed: @ "blankpage_image_loadFail"]; _ tipLabel. text = @ "It seems that something went wrong \ n is really sad";} else {// blank data if (_ reloadButton) {_ reloadButton. hidden = YES;} NSString * ImageName, * tipStr; switch (blankPageType) {case EaseBlankPageTypeActivity: // project dynamics {imageName = @ "blankpage_image_Sleep "; tipStr = @ "nothing here \ n hurry up to make a little noise";} break; case easeblkpagetypetask: // task list {imageName = @ "blancerpage_image_sleep "; tipStr = @ "there are no tasks here \ n hurry up to contribute to the team";} break; case easeblkpagetypetopic: // discussion list {imageName = @ "blankpage_image_Sleep "; tipStr = @ "How can I send a discussion here \ n to make it lively?";} break; Case EaseBlankPageTypeTweet: // bubble list (your own) {imageName = @ "blankpage_image_Hi"; tipStr = @ "no bubbles \ n, take a bubble ~ ";} Break; case EaseBlankPageTypeTweetOther: // bubble list (Others) {imageName = @" blankpage_image_Sleep "; tipStr = @" This person is lazy \ n a bubble has ~ ";} Break; case EaseBlankPageTypeProject: // Project List (your own) {imageName = @" blankpage_image_Sleep "; tipStr = @" You still have projects. Please hurry up and create them ~ ";} Break; case EaseBlankPageTypeProjectOther: // Project List (others') {imageName = @" blankpage_image_Sleep "; tipStr = @" This person is very lazy. Every project has ~ ";} Break; case EaseBlankPageTypeFileDleted: // The file page is displayed, and the file has been deleted {imageName = @" blankpage_image_loadFail "; tipStr = @ "the object \ n has just been deleted ~ ";} Break; case easeblkpagetypefolderdleted: // folder {imageName = @" blakpage_image_loadfail "; tipStr = @" The \ n folder seems to have been deleted ~ ";} Break; case easeblkpagetypeprivatemsg: // Private Message list {imageName = @" blakpage_image_hi "; tipStr = @" \ n say Hello ~ ";} Break; default: // other pages (all the pages not mentioned here belong to others) {imageName = @" blankpage_image_Sleep "; tipStr = @ "nothing here \ n hurry up to make a little noise";} break;} [_ monkeyView setImage: [UIImage imageNamed: imageName]; _ tipLabel. text = tipStr ;}}- (void) reloadButtonClicked :( id) sender {self. hidden = YES; [self removeFromSuperview]; dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (0.2 * NSEC_PER_SEC), then (), ^ {if (_ reloadButtonBlock) {_ reloadButtonBlock (sender) ;}}) ;}@ end

Note: There are two types: one is displayed when an error occurs in the request, and one is refreshed. The other is displayed when there is no data in the current table and classified;

2: extend the classification in UIView + Common.

#pragma mark BlankPageView@property (strong, nonatomic) EaseBlankPageView *blankPageView;- (void)configBlankPage:(EaseBlankPageType)blankPageType hasData:(BOOL)hasData hasError:(BOOL)hasError reloadButtonBlock:(void(^)(id sender))block;@end
- (void)setBlankPageView:(EaseBlankPageView *)blankPageView{    [self willChangeValueForKey:@"BlankPageViewKey"];    objc_setAssociatedObject(self, &BlankPageViewKey,                             blankPageView,                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);    [self didChangeValueForKey:@"BlankPageViewKey"];}- (EaseBlankPageView *)blankPageView{    return objc_getAssociatedObject(self, &BlankPageViewKey);}- (void)configBlankPage:(EaseBlankPageType)blankPageType hasData:(BOOL)hasData hasError:(BOOL)hasError reloadButtonBlock:(void (^)(id))block{    if (hasData) {        if (self.blankPageView) {            self.blankPageView.hidden = YES;            [self.blankPageView removeFromSuperview];        }    }else{        if (!self.blankPageView) {            self.blankPageView = [[EaseBlankPageView alloc] initWithFrame:self.bounds];        }        self.blankPageView.hidden = NO;        [self.blankPageContainer addSubview:self.blankPageView];//        [self.blankPageContainer insertSubview:self.blankPageView atIndex:0];//        [self.blankPageView mas_makeConstraints:^(MASConstraintMaker *make) {//            make.size.equalTo(self);//            make.top.left.equalTo(self.blankPageContainer);//        }];        [self.blankPageView configWithType:blankPageType hasData:hasData hasError:hasError reloadButtonBlock:block];    }}- (UIView *)blankPageContainer{    UIView *blankPageContainer = self;    for (UIView *aView in [self subviews]) {        if ([aView isKindOfClass:[UITableView class]]) {            blankPageContainer = aView;        }    }    return blankPageContainer;}

Note: Only the table view can be used to determine when the parent view is loaded. The background view is added and deleted;

3: The page call time code is as follows:

-(Void) refresh {if (_ isLoading) {return;} if (! _ CurCommitInfo) {[self. view beginLoading];} _ isLoading = YES; _ weak typeof (self) weakSelf = self; [[Coding_NetAPIManager sharedManager] metadata: _ ownerGK projectName: _ projectName commitId: _ commitId andBlock: ^ (CommitInfo * data, NSError * error) {weakSelf. isLoading = NO; [weakSelf. view endLoading]; [weakSelf. myRefreshControl endRefreshing]; if (data) {weakSelf. curCommitInfo = Data; [weakSelf configListGroups]; [weakSelf. myTableView extends mselector: @ selector (reloadData) withObject: nil afterDelay: 0.5];} [weakSelf. view configBlankPage: easeblkpagetypeview hasData :( weakSelf. curCommitInfo. commitDetail! = Nil) hasError :( error! = Nil) reloadButtonBlock: ^ (id sender) {[weakSelf refresh] ;}]; // if (! _ CurProject) {_ curProject = [Project new]; _ curProject. owner_user_name = _ ownerGK; _ curProject. name = _ projectName;} if (! [_ CurProject. id isKindOfClass: [NSNumber class]) {[[Coding_NetAPIManager sharedManager] request_ProjectDetail_WithObj: _ curProject andBlock: ^ (id data, NSError * error) {if (data) {weakSelf. curProject = data ;}}] ;}}

Note: directly request the configBlankPage of the call view.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.