iOS 關乎使用AFNetworking請求,三種占位圖。

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];
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容