主要参考了:http://www.oschina.net/code/snippet_1771722_44835 的内容。
#pragma mark 以渐渐消失的方式显示信息
-(void)showMessage:(NSString *)message
{
//This property holds the UIWindow object in the windows array that is most recently sent the makeKeyAndVisible message.
//保持最近发送makeKeyAndVisible信息的window,即正在显示的窗口
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIView *showView = [[UIView alloc]init];
showView.backgroundColor = [UIColor blackColor];
//先占位
showView.frame = CGRectMake(1, 1, 1, 1);
showView.layer.cornerRadius = 10.0f;
showView.layer.masksToBounds = YES;
[window addSubview:showView];
UILabel *_label = [[UILabel alloc]init];
UIFont *font = [UIFont boldSystemFontOfSize:15];
_label.font = font;
CGSize labelSize = [message sizeWithAttributes:@{NSFontAttributeName:font}];
_label.frame = CGRectMake(10, 5, labelSize.width, labelSize.height);
_label.text = message;
_label.textColor = [UIColor whiteColor];
_label.textAlignment = NSTextAlignmentCenter;
_label.backgroundColor = [UIColor clearColor];
[showView addSubview:_label];
// showView.clipsToBounds = YES;
showView.frame = CGRectMake((SCREEN_WIDTH - labelSize.width - 20)/2, SCREEN_HEIGHT - 240, labelSize.width + 10, labelSize.height + 10);
[UIView animateWithDuration:3 animations:^{
showView.alpha = 0;
} completion:^(BOOL finished) {
[showView removeFromSuperview];
}];
}
效果如下所示: