学习iOS技术iOS开发指南iOS应用框架

iOS浅谈如何设计第三方框架

2016-01-02  本文已影响1150人  船长_

如何设计一个好的第三方框架给别其他开发者用?需要要考虑哪些方面?

demo.gif

一.框架的名字

二.设计对外的接口

1.外界调用的方法

- (IBAction)success {
    [DXStatusBarHUD showSuccess:@"加载数据成功!"];
}

- (IBAction)error {
    [DXStatusBarHUD showError:@"登录失败!"];
}

- (IBAction)loading {
    [DXStatusBarHUD showLoading:@"正在登录中..."];
}

- (IBAction)hide {
    [DXStatusBarHUD hide];
}

- (IBAction)normal {
    [DXStatusBarHUD showText:@"随便显示的文字!!!!"];
}

2.对外提供的方法

/*此方法设计缺点:只能加载本地图片,不能加载网络图片,例如用SD_webImage下载保存到沙盒的图片
 * 显示图片+文字信息
 */
+ (void)showImageName:(NSString *)imageName text:(NSString *)text;

/**
 * 显示图片+文字信息
 */
+ (void)showImage:(UIImage *)image text:(NSString *)text;

/**
 * 显示成功信息
 */
+ (void)showSuccess:(NSString *)text;

/**
 * 显示失败信息
 */
+ (void)showError:(NSString *)text;

/**
 * 显示正在处理的信息
 */
+ (void)showLoading:(NSString *)text;

/**
 * 显示普通信息
 */
+ (void)showText:(NSString *)text;

/**
 * 隐藏
 */
+ (void)hide;

3.方法的实现

static UIWindow *window_;
static NSTimer *timer_;
/** HUD控件的高度 */
static CGFloat const DXWindowH = 20;
/** HUD控件的动画持续时间(出现\隐藏) */
static CGFloat const DXAnimationDuration = 0.25;
/** HUD控件默认会停留多长时间 */
static CGFloat const DXHUDStayDuration = 1.5;

+ (void)showImage:(UIImage *)image text:(NSString *)text
{
    // 停止之前的定时器
    [timer_ invalidate];
    
    // 创建窗口
    window_.hidden = YES; // 先隐藏之前的window
    window_ = [[UIWindow alloc] init];
    window_.backgroundColor = [UIColor blackColor];
    window_.windowLevel = UIWindowLevelAlert;
    window_.frame = CGRectMake(0, - DXWindowH, [UIScreen mainScreen].bounds.size.width, DXWindowH);
    window_.hidden = NO;
    
    // 添加按钮
    UIButton *button = [[UIButton alloc] init];
    button.frame = window_.bounds;
    // 文字
    [button setTitle:text forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    // 图片
    if (image) {
        [button setImage:image forState:UIControlStateNormal];
        button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5);
        button.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    }
    [window_ addSubview:button];
    
    // 动画
    [UIView animateWithDuration:DXAnimationDuration animations:^{
        CGRect frame = window_.frame;
        frame.origin.y = 0;
        window_.frame = frame;
    }];
    
    // 开启一个新的定时器
    timer_ = [NSTimer scheduledTimerWithTimeInterval:DXHUDStayDuration target:self selector:@selector(hide) userInfo:nil repeats:NO];
}

+ (void)showImageName:(NSString *)imageName text:(NSString *)text
{
    [self showImage:[UIImage imageNamed:imageName] text:text];
}

+ (void)showSuccess:(NSString *)text
{
    [self showImageName:@"DXStatusBarHUD.bundle/success" text:text];
}

+ (void)showError:(NSString *)text
{
    [self showImageName:@"DXStatusBarHUD.bundle/error" text:text];
}

+ (void)showText:(NSString *)text
{
    [self showImage:nil text:text];
}

+ (void)showLoading:(NSString *)text
{
    // 停止之前的定时器
    [timer_ invalidate];
    timer_ = nil;
    
    // 创建窗口
    window_.hidden = YES; // 先隐藏之前的window
    window_ = [[UIWindow alloc] init];
    window_.backgroundColor = [UIColor blackColor];
    window_.windowLevel = UIWindowLevelAlert;
    window_.frame = CGRectMake(0, - DXWindowH, [UIScreen mainScreen].bounds.size.width, DXWindowH);
    window_.hidden = NO;
    
    // 添加按钮
    UIButton *button = [[UIButton alloc] init];
    button.frame = window_.bounds;
    // 文字
    [button setTitle:text forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    [window_ addSubview:button];
    
    // 圈圈
    UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    [loadingView startAnimating];
    loadingView.center = CGPointMake(button.titleLabel.frame.origin.x - 60, DXWindowH * 0.5);
    [window_ addSubview:loadingView];
    
    // 动画
    [UIView animateWithDuration:DXAnimationDuration animations:^{
        CGRect frame = window_.frame;
        frame.origin.y = 0;
        window_.frame = frame;
    }];
}

+ (void)hide
{
    // 清空定时器
    [timer_ invalidate];
    timer_ = nil;
    
    // 退出动画
    [UIView animateWithDuration:DXAnimationDuration animations:^{
        CGRect frame = window_.frame;
        frame.origin.y =  - DXWindowH;
        window_.frame = frame;
    } completion:^(BOOL finished) {
        window_ = nil;
    }];
}

三.关于图片资源处理

pic.png

四.简单易用

上一篇 下一篇

猜你喜欢

热点阅读