#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface ZMStatuBarHud : NSObject
//成功时候显示消息
+ (void)showSuccess:(NSString *)msg;
//失败的时候显示消息
+ (void)showError:(NSString *)msg;
//加载的时候显示 调用此方法想要隐藏需自己调用hide方法
+ (void)showLoading:(NSString *)msg;
//隐藏提示框
+ (void)hide;
//显示信息
+ (void)showMessage:(NSString *)msg;
//自定义提示框
+ (void)showMessage:(NSString *)msg image:(UIImage *)image;

@end

//
//  ZMStatuBarHud.m
//  状态栏HUD232
//
//  Created by 张明 on 16/3/7.
//  Copyright © 2016年 张明. All rights reserved.
//
#define MMfont [UIFont systemFontOfSize:13]
#import "ZMStatuBarHud.h"
static UIWindow *window_;

static NSTimer *timer_;

//消息动画隐藏时间
static CGFloat const MMMessageDuration = 0.25;

@implementation ZMStatuBarHud
/*创建窗口*/
+ (void)setUpWindow
{
    CGFloat windowH = 20;
    
    CGRect frame = CGRectMake(0, -windowH, [UIScreen mainScreen].bounds.size.width, windowH);
    
    window_.hidden = YES;
    
    window_ = [[UIWindow alloc]init];
    
    window_.frame = frame;
    
    window_.hidden = NO;
    
    window_.windowLevel = UIWindowLevelAlert;
    
    window_.backgroundColor = [UIColor blackColor];
    
    frame.origin.y = 0;
    
    [UIView animateWithDuration:MMMessageDuration animations:^{
        window_.frame = frame;
    }];
 
}

+ (void)showMessage:(NSString *)msg image:(UIImage *)image
{
    [timer_ invalidate];
    
    //添加按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button setImage:image forState:UIControlStateNormal];
    
    [button setTitle:msg forState:UIControlStateNormal];
    
    button.titleLabel.font = MMfont;
    
    if (image) {
        button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    }
    
    button.frame = window_.bounds;
    
    [window_ addSubview:button];
    
    timer_ = [NSTimer scheduledTimerWithTimeInterval:MMMessageDuration target:self selector:@selector(hide) userInfo:nil repeats:NO];
    
}

+ (void)hide
{
    [UIView animateWithDuration:MMMessageDuration animations:^{
        CGRect frame = window_.frame;
        frame.origin.y = -frame.size.height;
        
        window_.frame = frame;
        
        
    } completion:^(BOOL finished) {
        window_ = nil;
        timer_ = nil;
        
        
    }];
    
}

+ (void)showMessage:(NSString *)msg
{
    [self showMessage:msg image:nil];
}

+ (void)showSuccess:(NSString *)msg
{
    
    [self showMessage:msg image:[UIImage imageNamed:@"check"]];
}

+ (void)showError:(NSString *)msg
{
    
    [self showMessage:msg image:[UIImage imageNamed:@"fault"]];
}

+ (void)showLoading:(NSString *)msg
{
    [timer_ invalidate];
    
    timer_ = nil;
    
    [self setUpWindow];
    
    UILabel *label = [[UILabel alloc]init];
    
    label.font = MMfont;
    
    label.frame = window_.bounds;
    
    label.textAlignment = NSTextAlignmentCenter;
    
    label.text = msg;
    
    label.textColor = [UIColor whiteColor];
    
    [window_ addSubview:label];
    
    //加载圈圈
    UIActivityIndicatorView *loadView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    
    [loadView startAnimating];
    
    CGFloat msgW = [msg sizeWithAttributes:@{NSFontAttributeName:MMfont}].width;
    CGFloat centX = (window_.frame.size.width - msgW)*0.5 -20;
    
    CGFloat centY = window_.frame.size.height*0.5;
    
    loadView.center = CGPointMake(centX, centY)
    ;
    
    [window_ addSubview:loadView];
    
}

@end

通过自定义window来实现提示框效果的更多相关文章

  1. 【转】提示框第三方库之MBProgressHUD iOS toast效果 动态提示框效果

    原文网址:http://www.zhimengzhe.com/IOSkaifa/37910.html MBProgressHUD是一个开源项目,实现了很多种样式的提示框,使用上简单.方便,并且可以对显 ...

  2. JS组件Bootstrap实现弹出框和提示框效果代码

    这篇文章主要介绍了JS组件Bootstrap实现弹出框和提示框效果代码,对弹出框和提示框感兴趣的小伙伴们可以参考一下 前言:对于Web开发人员,弹出框和提示框的使用肯定不会陌生,比如常见的表格新增和编 ...

  3. 单一按钮显示/隐藏&&提示框效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. WPF提示框效果

    WPF提示框效果 1,新建WPF应用程序 2,添加用户控件Message 3,在Message中编写如下代码 <Border x:Name="border" BorderTh ...

  5. div+css实现圆形div以及带箭头提示框效果

    .img{ width:90px; height:90px; border-radius:45px; margin:0 40%; border:solid rgb(100,100,100) 1px;& ...

  6. 基于JQuery 的消息提示框效果代码

    提示框效果 一下是封装到 Jquery.L.Message.js 中的JS文件内容 var returnurl = ''; var messagebox_timer; $.fn.messagebox ...

  7. JS组件系列——Bootstrap寒冬暖身篇:弹出框和提示框效果以及代码展示

    前言:对于Web开发人员,弹出框和提示框的使用肯定不会陌生,比如常见的表格新增和编辑功能,一般常见的主要有两种处理方式:行内编辑和弹出框编辑.在增加用户体验方面,弹出框和提示框起着重要的作用,如果你的 ...

  8. Bootstrap:弹出框和提示框效果以及代码展示

    前言:对于Web开发人员,弹出框和提示框的使用肯定不会陌生,比如常见的表格新增和编辑功能,一般常见的主要有两种处理方式:行内编辑和弹出框编辑.在增加用户体验方面,弹出框和提示框起着重要的作用,如果你的 ...

  9. Bootstrap实现弹出框和提示框效果代码

    一.Bootstrap弹出框使用过JQuery UI应该知道,它里面有一个dialog的弹出框组件,功能也很丰富.与jQuery UI的dialog类似,Bootstrap里面也内置了弹出框组件.打开 ...

随机推荐

  1. 对“xxx”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们。

    在程序中调用C++链接库中的回调函由于没有考虑生命周期,直接写委托回随机的被gc给回收掉导致报这个错误 错误的程序: private void InitPlateIdentify() { try { ...

  2. 在VC中,为图片按钮添加一些功能提示(转)

    在VC中,也常常为一些图片按钮添加一些功能提示.下面讲解实现过程:该功能的实现主要是用CToolTipCtrl类.该类在VC  msdn中有详细说明.首先在对话框的头文件中加入初始化语句:public ...

  3. BZOJ 2819: Nim( nim + DFS序 + 树状数组 + LCA )

    虽然vfleaking好像想卡DFS...但我还是用DFS过了... 路径上的石堆异或和=0就是必败, 否则就是必胜(nim游戏). 这样就变成一个经典问题了, 用DFS序+BIT+LCA就可以在O( ...

  4. SGU 187.Twist and whirl - want to cheat( splay )

    维护一个支持翻转次数M的长度N的序列..最后输出序列.1<=N<=130000, 1<=M<=2000 splay裸题... ------------------------- ...

  5. nodejs取得mac地址

            1.背景        使用nodejs取得客户端电脑的mac地址作为唯一的标识,但如何使用getmac模块,        本文结合网上资料和实践,总结如下: 2.需要的moduel ...

  6. 常用Json

    一般Json是页面与页面之间传递使用. Json用途        1 后台与前台数据交互,并且数据较复杂,如果数据单一,直接传递字符串,然后在前台用js分割就行. 2 webservice和html ...

  7. 框架开发(三)---smarty整合

    一 smarty 是什么 Smarty是一个PHP的模板引擎.更明确来说,它可以帮助开发者更好地 分离程序逻辑和页面显示.最好的例子,是当程序员和模板设计师是不同的两个角色的情况,而且 大部分时候都不 ...

  8. PyCrpyto windows安装使用方法

    PyCrypto - The Python Cryptography Toolkit PyCrypto是一个免费的加密算法库,支持常见的DES.AES加密以及MD5.SHA各种HASH运算. ---- ...

  9. 利用python进行数据分析之数据加载存储与文件格式

    在开始学习之前,我们需要安装pandas模块.由于我安装的python的版本是2.7,故我们在https://pypi.python.org/pypi/pandas/0.16.2/#downloads ...

  10. Oracle 数据库导入、导出

    第一步:新建一个txt文件: exp.exe jeamsluu@test file=d:\daochu.dmp log=1.log 另存为.bat格式的文件 第二步:双击运行:此时会弹出输入口令的对话 ...