通过自定义window来实现提示框效果
Posted 听风观雨阁
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过自定义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
以上是关于通过自定义window来实现提示框效果的主要内容,如果未能解决你的问题,请参考以下文章
WPF自定义控件与样式(13)-自定义窗体Window & 自适应内容大小消息框MessageBox