恩美第二个APP项目控件类

iOS 关乎使用AFNetworking请求,三种占位图。

2017-08-14  本文已影响202人  雪_晟

1、等待网络请求的占位图。

1.png

借助MBProgressHUD自定义view实现。

[MBProgressHUD show:@"" icon:@"矢量智能对象" view:self.view];

+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
//    hud.label.text = text;
      hud.mode = MBProgressHUDModeCustomView;
    // 设置图片
    UIImage *image =[UIImage imageNamed:icon];
    hud.customView = [[UIImageView alloc] initWithImage:image];    // 再设置模式
  
    hud.bezelView.backgroundColor =[UIColor whiteColor];
    hud.backgroundView.backgroundColor =[UIColor whiteColor];
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    
}

网络异常重新加载的占位图,以及无数据的占位图。

2.png 3.png

对比了一下Reachability以及AFNetWorkingAFNetworkReachabilityManager两种检测网路的方法,感觉都不是很准。
只能借助网络请求之后的错误码code,当code-1099时,AFNetWorking提示网络断开连接。可以根据 返回code码做文章。

使用分类来给view扩展占位图。关于扩展属性,可以了解运行时。

#import <UIKit/UIKit.h>
typedef void (^ReGetNetwork)(void);
@interface UIView (NCPlaceholder)
@property(nonatomic,copy)ReGetNetwork reGetwork; //重新刷新网络的回调
-(void)showNoNetworkViewWithBlock:(ReGetNetwork)reGetwork;//无网络时点击重新刷新网络
-(void)showNoDataView;//无数据view

-(void)hideNoNetworkView;
-(void)hideNoDataView;

@end

#import "UIView+NCPlaceholder.h"
#import <objc/runtime.h>
#import "NCNoNetWorkView.h"
#define Device_Width  [[UIScreen mainScreen] bounds].size.width//获取屏幕宽高
#define Device_Height [[UIScreen mainScreen] bounds].size.height
@implementation UIView (NCPlaceholder)
-(void)showNoNetworkViewWithBlock:(ReGetNetwork)reGetwork{
    
    
    NCNoNetWorkView *noView =[[NSBundle mainBundle]loadNibNamed:@"NCNoNetWorkView" owner:self options:nil].firstObject;
    noView.tag = 2593;
    noView.frame = CGRectMake(0, 64, Device_Width, Device_Height - 64);
    
    noView.imageView.image =[UIImage imageNamed:@"nothing"];
    noView.noticeTitle.text = @"当前网络无连接";
    noView.reLoadBtn.hidden = NO;
    [noView.reLoadBtn addTarget:self action:@selector(reLoadBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:noView];
    
    self.reGetwork = reGetwork;
    
    
}
-(void)reLoadBtnAction{
    
    if (self.reGetwork) {
        self.reGetwork();
    }
}
-(void)showNoDataView{
    
    NCNoNetWorkView *noView =[[NSBundle mainBundle]loadNibNamed:@"NCNoNetWorkView" owner:self options:nil].firstObject;
    
    noView.frame = CGRectMake(0, 64, Device_Width, Device_Height - 64);
    noView.tag = 2592;
    noView.imageView.image =[UIImage imageNamed:@"nothing"];
    noView.noticeTitle.text = @"无数据";
    noView.reLoadBtn.hidden = YES;
    [self addSubview:noView];
    
    
}
-(void)hideNoNetworkView{
     NCNoNetWorkView *noView = (NCNoNetWorkView *)[self viewWithTag:2593];
    if (noView) {
        [noView removeFromSuperview];
    }
}
-(void)hideNoDataView{
    NCNoNetWorkView *noDataView = (NCNoNetWorkView *)[self viewWithTag:2592];
    if (noDataView) {
        [noDataView removeFromSuperview];
    }
}
-(ReGetNetwork)reGetwork{
    
    return objc_getAssociatedObject(self, @selector(reGetwork));
}
-(void)setReGetwork:(ReGetNetwork)reGetwork{
    objc_setAssociatedObject(self, @selector(reGetwork), reGetwork, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
@end

使用方法:

1、网络异常重新加载的占位图:
 [self.view showNoNetworkViewWithBlock:^{
        
        NSLog(@"%@",[weakSelf class]); //可重新请求网络
        
        [weakSelf.view hideNoNetworkView];
        
      
    }];
2、网络无数据的占位图:
[self.view showNoDataView];
上一篇下一篇

猜你喜欢

热点阅读