码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2016-03-07 22:21:55      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

#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来实现提示框效果

标签:

原文地址:http://www.cnblogs.com/zmloveworld/p/5251867.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!