通过自定义window来实现提示框效果

#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

时间: 2024-08-05 19:35:57

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

提示框第三方库之MBProgressHUD iOS toast效果 动态提示框效果

提示框第三方库之MBProgressHUD iOS toast效果 动态提示框效果 2014-08-11 17:39 11614人阅读 评论(0) 收藏 举报  分类: iOS相关(20)  文章来自:http://blog.csdn.net/ryantang03/article/details/7877120 MBProgressHUD是一个开源项目,实现了很多种样式的提示框,使用上简单.方便,并且可以对显示的内容进行自定义,功能很强大,很多项目中都有使用到.到GitHub上可以下载到项目源码

确定和取消提示框效果

确定和取消提示框效果:在很多网页都有这样的效果,当点击一个按钮或者其他的对象会弹出一个提示框,如果点击确定则继续执行既定的程序,如果点击取消则会取消继续执行,代码实例如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="auth

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

JS组件Bootstrap实现弹出框和提示框效果代码 作者:懒得安分 字体:[增加 减小] 类型:转载 时间:2015-12-08我要评论 这篇文章主要介绍了JS组件Bootstrap实现弹出框和提示框效果代码,对弹出框和提示框感兴趣的小伙伴们可以参考一下 前言:对于Web开发人员,弹出框和提示框的使用肯定不会陌生,比如常见的表格新增和编辑功能,一般常见的主要有两种处理方式:行内编辑和弹出框编 辑.在增加用户体验方面,弹出框和提示框起着重要的作用,如果你的系统有一个友好的弹出提示框,自然能给用户

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

原文:JS组件系列--Bootstrap寒冬暖身篇:弹出框和提示框效果以及代码展示 前言:对于Web开发人员,弹出框和提示框的使用肯定不会陌生,比如常见的表格新增和编辑功能,一般常见的主要有两种处理方式:行内编辑和弹出框编辑.在增加用户体验方面,弹出框和提示框起着重要的作用,如果你的系统有一个友好的弹出提示框,自然能给用户很好的页面体验.前面几章介绍了bootstrap的几个常用组件,这章来看看bootstrap里面弹出框和提示框的处理.总的来说,弹出提示主要分为三种:弹出框.确定取消提示框.信

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

(转自Bootstrap:弹出框和提示框效果以及代码展示) 前言:对于Web开发人员,弹出框和提示框的使用肯定不会陌生,比如常见的表格新增和编辑功能,一般常见的主要有两种处理方式:行内编辑和弹出框编辑.在增加用户体验方面,弹出框和提示框起着重要的作用,如果你的系统有一个友好的弹出提示框,自然能给用户很好的页面体验.前面几章介绍了bootstrap的几个常用组件,这章来看看bootstrap里面弹出框和提示框的处理.总的来说,弹出提示主要分为三种:弹出框.确定取消提示框.信息提示框.本篇就结合这三

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

提示框效果 一下是封装到 Jquery.L.Message.js 中的JS文件内容 var returnurl = ''; var messagebox_timer; $.fn.messagebox = function (message, url, type, delay) { clearTimeout(messagebox_timer); $("#msgprint").remove(); var m_body = $(this); delay = (typeof delay ==

悬浮提示框效果

var IeAuto = navigator.userAgent.indexOf('MSIE') > -1; //判断是不是Ie浏览器 并赋值给变量 window.onload=function(){ function show(obj,id,html,width,height){ if(document.getElementById(id)==null){ //判断创建出来的div的id是否为空 var div = document.createElement('div'); //创建div

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

前言:对于Web开发人员,弹出框和提示框的使用肯定不会陌生,比如常见的表格新增和编辑功能,一般常见的主要有两种处理方式:行内编辑和弹出框编辑.在增加用户体验方面,弹出框和提示框起着重要的作用,如果你的系统有一个友好的弹出提示框,自然能给用户很好的页面体验.前面几章介绍了bootstrap的几个常用组件,这章来看看bootstrap里面弹出框和提示框的处理.总的来说,弹出提示主要分为三种:弹出框.确定取消提示框.信息提示框.本篇就结合这三种类型分别来介绍下它们的使用. 一.Bootstrap弹出框

仿百度搜索提示框效果

<!doctype html><html><head> <meta charset="UTF-8"> <title>百度搜索提示框</title> <style> * { margin: 0;padding: 0; outline: none;} .search101 { width: 650px; margin: 300px auto; font-size: 0; } .sou1 { width: 5