iOS 中经常会遇到这样的情况,比如网络连接错误,需要弹出一段文字提示,稍后自动关闭
比如登陆成功提示登陆完成,稍后关闭
如图
具体实现如下,我们创建一个单例,后续使用只需要复制代码就行
创建一个类起名 MyAlertCenter 控制提示信息的现实与关闭
同时,创建内部类 MyAlert 继承UIView 现实提示内容
具体代码如下
.h文件
#import <UIKit/UIKit.h>
@interface MyAlert : UIView
-(id)init;
- (void) setMessageText:(NSString *)message;
@end
@interface MyAlertCenter : NSObject{
MyAlert *myAlertView;//alertView
BOOL active;//当前是否在用
}
+ (MyAlertCenter *) defaultCenter;//单例 生成alert控制器
- (void) postAlertWithMessage:(NSString*)message;//弹出文字
@end
.m文件
#import "MyAlertCenter.h"
@implementation MyAlertCenter
+ (MyAlertCenter *) defaultCenter{
static MyAlertCenter *defaultCenter;
if (!defaultCenter) {
defaultCenter=[[MyAlertCenter alloc]init];
}
return defaultCenter;
}
- (id) init{
if(!(self=[super init])) return nil;
myAlertView = [[MyAlert alloc] init];
myAlertView.alpha = 0.0f;
active = NO;
[[UIApplication sharedApplication].keyWindow addSubview:myAlertView];
return self;
}
- (void) postAlertWithMessage:(NSString*)message{
//判断当前是否在使用中
if (!active) {
[self showAlerts:message];
}
}
- (void) showAlerts:(NSString *) str {
//开始使用,设置当前为使用状态
active = YES;
myAlertView.alpha = 0;
[[UIApplication sharedApplication].keyWindow addSubview:myAlertView];
[myAlertView setMessageText:str];
myAlertView.center = [UIApplication sharedApplication].keyWindow.center;
//设置动画
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationStep2)];
myAlertView.alpha = 0.8;
[UIView commitAnimations];
}
- (void) animationStep2{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelay:1.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationStep3)];
myAlertView.alpha = 0;
[UIView commitAnimations];
}
- (void) animationStep3{
[myAlertView removeFromSuperview];
active=NO;
}
@end
@implementation MyAlert
CGRect messageRect;
NSString *text;
-(id)init{
self=[super initWithFrame:CGRectMake(0, 0, 100, 10)];
if (self) {
messageRect =CGRectInset(self.bounds, 10, 10);
}
return self;
}
-(void)setMessageText:(NSString *)message{
text=message;
NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};
CGSize s=[text boundingRectWithSize:CGSizeMake(100, 0) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
self.bounds = CGRectMake(0, 0, s.width+40, s.height+15+15);
messageRect.size = s;
messageRect.size.height += 5;
messageRect.origin.x = 20;
messageRect.origin.y = 15;
[self setNeedsLayout];
[self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect
{
NSDictionary* attrs =@{NSForegroundColorAttributeName:[UIColor whiteColor]
,NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:18]
};
[text drawInRect:messageRect withAttributes:attrs]; //给文本限制个矩形边界,防止矩形拉伸;
}
@end好了,MyAlertCenter 类 实现完成了。
如果需要使用的话 我们直接这样调用 即可
[[MyAlertCenter defaultCenter] postAlertWithMessage:@"登陆成功"];
如果说我们需要调整提示内容现实的时间
可以找
- (void) animationStep2{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelay:1.0]; //显示时间
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationStep3)];
myAlertView.alpha = 0;
[UIView commitAnimations];
}
如果要调整 提示显示的位置(示例中默认现实在屏幕最中间)
可以找
myAlertView.center = [UIApplication sharedApplication].keyWindow.center; //设置位置
苹果开发群 :414319235 欢迎加入 欢迎讨论问题
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/lwjok2007/article/details/47422093