美文网首页
链式编程

链式编程

作者: Thinkdifferents | 来源:发表于2017-09-05 01:33 被阅读0次

站在巨人的肩膀上开发
SDAutoLayout
Masonry
LEEAlert

借鉴此写法,用链式编程思想封装了 MBProgressAFNetworking

Network - Demo

封装了 网络层 和 数据层

Network.netConfig
    .url(@"url")
    .params(@{@"" : @"参数"})
    .body(@{@"" : @"参数需要放body的话"})
    .downloadProgress(^(NSProgress * _Nonnull progress) {
        // 下载进度
    })
    .uploadProgress(^(NSProgress * _Nonnull progress) {
        // 上传进度
    })
    .netSuccessBlock(^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 网络成功
    })
    .netErrorBlock(^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 网络失败
    })
    .dataSuccessBlock(^(id  _Nonnull data) {
        // 数据解析成功
    })
    .dataErrorBlock(^(NSString * _Nonnull errCode, NSString * _Nonnull errmsg) {
        // 数据解析失败
    })
    .getRequest();

MBProgress

使用方式

// 展示纯文字
HUD.instance.ShowText(@"text");

// 展示loading
HUD.instance.ShowLoading();


 // 自定义
HUD.instance
.BackgroundColor(RGBA(0, 0, 0, 0.55))
.TipDuration(2.5)
.Radius(0)
.ContentInset(13)
.HalfCorner(true)
.ShowText(@"每日任务\n观看15s,+35金豆                          \n每日三次");

就不传demo了 -- .h

//
//  HUD.h
//  InfrastructureProjects
//
//  Created by shiwei on 2018/4/23.
//  Copyright © 2018 shiwei. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@class HUD;

NS_ASSUME_NONNULL_BEGIN

typedef HUD * _Nonnull (^HUDToVoid) (void);
typedef HUD * _Nonnull (^HUDToString) (NSString *text);
typedef HUD * _Nonnull (^HUDToFloat) (CGFloat duration);
typedef HUD * _Nonnull (^HUDToColor) (UIColor *backgroundColor);
typedef HUD * _Nonnull (^HUDToColor) (UIColor *tinkColor);
typedef HUD * _Nonnull (^HUDToView) (UIView *view);
typedef HUD * _Nonnull (^HUDToBoolean) (Boolean halfCorner);

@interface HUD : NSObject

/// 提示时间  默认 1.5
@property (nonatomic, copy, readonly) HUDToFloat TipDuration;

/// 等待时间 默认 0 一直持续 需要手动关闭
@property (nonatomic, copy, readonly) HUDToFloat LoadingDuration;
@property (nonatomic, copy, readonly) HUDToView ToView;

/// 圆角
@property (nonatomic, copy, readonly) HUDToFloat Radius;

/// 内边距
@property (nonatomic, copy, readonly) HUDToFloat ContentInset;

/// 是否为半角, 为True时  `Radius` 属性失效
@property (nonatomic, copy, readonly) HUDToBoolean HalfCorner;

/// 风格颜色
@property (nonatomic, copy, readonly) HUDToColor TinkColor;

/// 背景颜色
@property (nonatomic, copy, readonly) HUDToColor BackgroundColor;

@property (nonatomic, copy, readonly) HUDToString ShowSuccess;
@property (nonatomic, copy, readonly) HUDToString ShowError;
@property (nonatomic, copy, readonly) HUDToVoid ShowLoading;
@property (nonatomic, copy, readonly) HUDToString ShowText;
@property (nonatomic, copy, readonly) HUDToString ShowTextLoading;
@property (nonatomic, copy, readonly) HUDToVoid ShowCustomLoading;

+ (instancetype)instance;
+ (void)hide;

@end

NS_ASSUME_NONNULL_END


.m

//
//  HUD.m
//  InfrastructureProjects
//
//  Created by shiwei on 2018/4/23.
//  Copyright © 2018 shiwei. All rights reserved.
//

#import "HUD.h"
#import <MBProgressHUD/MBProgressHUD.h>

@interface HUD ()

@property (nonatomic, assign) CGFloat tipDuration;
@property (nonatomic, assign) CGFloat loadingDuration;
@property (nonatomic, strong) UIColor *tinkColor;
@property (nonatomic, strong) UIColor *backgroundColor;
@property (nonatomic, assign) CGFloat radius;
@property (nonatomic, assign) CGFloat contentInset;
@property (nonatomic, assign) Boolean halfCorner;
@property (nonatomic, weak) UIView *view;


@end

@implementation HUD

static MBProgressHUD *_hud;

+ (instancetype)instance {
    return [[HUD alloc] _init];
}

- (instancetype)_init
{
    self = [super init];
    if (self) {
        self.tipDuration = 1.5;
        self.loadingDuration = 0;
        self.view = [HUD windowView];
        self.tinkColor = UIColor.whiteColor;
        self.backgroundColor = UIColor.blackColor;
        self.radius = 0;
    }
    return self;
}

+ (UIView *)windowView {
    NSArray * arr = [UIApplication sharedApplication].windows;
    if (arr.count == 1) {
        return arr.lastObject;
    } else {
        UIWindow *win = arr.firstObject;
        for (UIWindow *window in arr) {
            if ([window isMemberOfClass:[UIWindow class]]) {
                win = window;
            }
        }
        return win;
    }
}

- (HUDToFloat)TipDuration {
    
    return ^(CGFloat duration) {
        
        self.tipDuration = duration;
        return self;
    };
}

- (HUDToFloat)LoadingDuration {
    
    return ^(CGFloat duration) {
        
        self.loadingDuration = duration;
        return self;
    };
}

- (HUDToView)ToView {
    
    return ^(UIView *view) {
        
        self.view = view;
        return self;
    };
}

- (HUDToFloat)Radius {
    return ^(CGFloat radius) {
        
        self.radius = radius;
        return self;
    };
}

- (HUDToFloat)ContentInset {
    return ^(CGFloat value) {
        
        self.contentInset = value;
        return self;
    };
}

- (HUDToColor)BackgroundColor {
    
    return ^(UIColor *color) {
        
        self.backgroundColor = color;
        return self;
    };
}

- (HUDToColor)TinkColor {
    
    return ^(UIColor *color) {
        
        self.tinkColor = color;
        return self;
    };
}

- (HUDToBoolean)HalfCorner {
    
    return ^(Boolean value) {
        
        self.halfCorner = value;
        return self;
    };
}

- (HUDToString)ShowSuccess {
    
    return ^(NSString *success) {
        
        [self show:success icon:@"HUD_success" toView:self.view afterDelay:self.tipDuration];
        return self;
    };
}

- (HUDToString)ShowError {
    
    return ^(NSString *error) {
        
        [self show:error icon:@"HUD_error" toView:self.view afterDelay:self.tipDuration];
        return self;
    };
}

- (HUDToString)ShowText {
    
    return ^(NSString *text) {
        
        [self show:text icon:nil toView:self.view afterDelay:self.tipDuration];
        return self;
    };
}

- (void)show:(NSString *)text icon:(NSString *)icon toView:(UIView *)view afterDelay:(CGFloat)duration {
    
//    __weak typeof(self) wself = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        
//        __strong typeof(self) self = wself;
        // 快速显示一个提示信息
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        hud.label.text = text;
        // 判断是否显示图片
        if (icon == nil) {
            hud.mode = MBProgressHUDModeText;
            hud.label.numberOfLines = 0;
        }else{
            // 设置图片
            UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]];
            img = img == nil ? [UIImage imageNamed:icon] : img;
            hud.customView = [[UIImageView alloc] initWithImage:img];
            // 再设置模式
            hud.mode = MBProgressHUDModeCustomView;
        }
        // 隐藏时候从父控件中移除
        hud.removeFromSuperViewOnHide = YES;
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.bezelView.color = self.backgroundColor ? self.backgroundColor : UIColor.blackColor;
        hud.contentColor = self.tinkColor ? self.tinkColor : UIColor.whiteColor;
        if (self.radius > 0) {
            hud.bezelView.layer.cornerRadius = self.radius;
        }
        if (self.contentInset > 0) {
            hud.margin = self.contentInset;
        }
        if (self.halfCorner) {
            [[hud rac_signalForSelector:@selector(layoutSubviews)] subscribeNext:^(RACTuple * _Nullable x) {
                [hud.superview layoutSubviews];
                hud.bezelView.layer.cornerRadius = ceil(hud.bezelView.bounds.size.height / 2.f);
            }];
        }
        // 指定时间之后再消失
        [hud hideAnimated:true afterDelay:duration];
    });
}


- (HUDToVoid)ShowLoading {
    
    return ^() {
        
        [self showNormalLoadingWithText:@""];
        return self;
    };
}

- (HUDToString)ShowTextLoading {
    
    return ^(NSString *text) {
        
        [self showNormalLoadingWithText:text];
        return self;
    };
}

- (void)showNormalLoadingWithText:(NSString *)text {
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = text;
    // 再设置模式
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;hud.bezelView.color = self.backgroundColor ? self.backgroundColor : UIColor.blackColor;
    hud.contentColor = self.tinkColor ? self.tinkColor : UIColor.whiteColor;
    if (self.radius > 0) {
        hud.bezelView.layer.cornerRadius = self.radius;
    }
    if (self.contentInset > 0) {
        hud.margin = self.contentInset;
    }
    if (self.halfCorner) {
        [[hud rac_signalForSelector:@selector(layoutSubviews)] subscribeNext:^(RACTuple * _Nullable x) {
            [hud.superview layoutSubviews];
            hud.bezelView.layer.cornerRadius = ceil(hud.bezelView.bounds.size.height / 2.f);
        }];
    }
    
    if (self.loadingDuration) {
        [hud hideAnimated:true afterDelay:self.loadingDuration];
    } else {
        if (_hud) {
            [_hud hideAnimated:true];
        }
        _hud = hud;
    }
}

- (HUDToVoid)ShowCustomLoading {
    
    return ^() {
        
        [self showHTJLoading];
        return self;
    };
}

- (void)showHTJLoading {
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = UIColor.blackColor;
    hud.contentColor = UIColor.whiteColor;
    hud.square = YES;
    hud.bezelView.color = [UIColor clearColor];
    hud.mode = MBProgressHUDModeCustomView;
    
    UIImage *image = [UIImage imageNamed:@"frame_00"];
    UIImageView *animateGifView = [[UIImageView alloc]initWithImage:image];
    
    NSMutableArray *gifArray = [NSMutableArray array];
    for (int i = 0; i <= 46; i ++) {
        UIImage *images = [UIImage imageNamed:[NSString stringWithFormat:@"frame_%02d",i]];
        if (images) {
            UIImage *temp = [HUD scaleToSize:images size:CGSizeMake(50, 50)];
            [gifArray addObject:temp];
        }
    }
    
    [animateGifView setAnimationImages:gifArray];
    [animateGifView setAnimationDuration:2.5];
    [animateGifView setAnimationRepeatCount:0];
    [animateGifView startAnimating];
    hud.customView = animateGifView;
    
    if (self.loadingDuration) {
        [hud hideAnimated:true afterDelay:self.loadingDuration];
    } else {
        if (_hud) {
            [_hud hideAnimated:true];
        }
        _hud = hud;
    }
}

+ (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)newsize{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    //原图片尺寸是150*150 当裁剪时会被压缩 所以改成3倍(50*50) 不然图片会不清晰
    UIGraphicsBeginImageContextWithOptions(newsize, NO, 3.0);
    // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0, 0, newsize.width, newsize.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    // 返回新的改变大小后的图片
    return scaledImage;
}

+ (void)hide {
    [_hud hideAnimated:true];
}

@end

相关文章

  • jQuery 链式编程

    jQuery 链式编程 链式编程原理 链式编程代码示例 隐式迭代

  • 链式编程思想

    链式编程思想 链式编程思想

  • iOS 链式编程

    链式编程 主要介绍链式编程原理,以及如何创建链式编程 编程范式 在介绍链式编程之前,首先来了解下什么是编程范式。 ...

  • ReactiveObjC入门

    ReactiveObjC基础用法 iOS开发三种编程方式(响应式编程、函数编程、链式编程),函数编程最常用,链式编...

  • 工作iOS技术总结

    链式编程、函数式、面向接口编程思想 iOS 之ReactiveCocoa 链式编程2 WKWebView的缓存处理...

  • 一、链式编程思想、响应式编程思想和函数式编程

    1. 链式编程思想 链式编程思想的核心就是方法的返回值必须是Block,Masonry是链式编程思想应用的代表。 ...

  • 链式编程和函数式编程的简单理解

    链式编程 此前看到SnipKit中运用了很多链式编程的代码,感觉非常好用:例如: //此段就是链式编程的典型代码m...

  • 编程思想-链式编程

    编程思想-链式编程 链式编程(链式调用)的主要表现是支持点语法结构的代码,代表库可以参照 Masonry 或 Sn...

  • 链式编程

    链式编程

  • iOS 链式编程

    之前看到 Masonry 是使用链式编程,感觉很酷,自己简单的写了一个链式编程的示例! 首先简单的来说一下链式编程...

网友评论

      本文标题:链式编程

      本文链接:https://www.haomeiwen.com/subject/qryidxtx.html