• 通过自定义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

  • 相关阅读:
    Cookie工具类
    验证工具类
    压缩工具类
    一次外企QQ面试
    利用Referer请求头阻止"盗链"
    servlet中ServletConfig的使用
    jquery插件制作
    jQuery选择器总结(转)
    js文件加载执行顺序
    mysql有关问题之:the security settings could not be applied to
  • 原文地址:https://www.cnblogs.com/zmloveworld/p/5251867.html
Copyright © 2020-2023  润新知