原文转自:http://blog.csdn.net/iBright/article/details/7078436
分享一个我写的类似于android的toast的提示框
主要特点:
1,支持屏幕Y轴任意位置显示,设置距离顶/底端距离
2,支持多行文本
3,支持设置等待时间
4,支持点击隐藏,屏幕旋转时自动隐藏,淡入淡出
5,无需初始化,类方法调用
效果图:
全部代码如下,使用时需要添加QuartzCore.framework,希望能给大家带来方便。
@interface OMGToast : NSObject {NSString *text;UIButton *contentView;CGFloat duration;
}+ (void)showWithText:(NSString *) text_;
+ (void)showWithText:(NSString *) text_duration:(CGFloat)duration_;+ (void)showWithText:(NSString *) text_topOffset:(CGFloat) topOffset_;
+ (void)showWithText:(NSString *) text_topOffset:(CGFloat) topOffsetduration:(CGFloat) duration_;+ (void)showWithText:(NSString *) text_bottomOffset:(CGFloat) bottomOffset_;
+ (void)showWithText:(NSString *) text_bottomOffset:(CGFloat) bottomOffset_duration:(CGFloat) duration_;@end
#import "OMGToast.h"
#import @interface OMGToast (private)- (id)initWithText:(NSString *)text_;
- (void)setDuration:(CGFloat) duration_;- (void)dismisToast;
- (void)toastTaped:(UIButton *)sender_;- (void)showAnimation;
- (void)hideAnimation;- (void)show;
- (void)showFromTopOffset:(CGFloat) topOffset_;
- (void)showFromBottomOffset:(CGFloat) bottomOffset_;@end@implementation OMGToast- (void)dealloc{[[NSNotificationCenter defaultCenter] removeObserver:selfname:UIDeviceOrientationDidChangeNotificationobject:[UIDevice currentDevice]];contentView = nil;text = nil;
}- (id)initWithText:(NSString *)text_{if (self = [super init]) {text = [text_ copy];UIFont *font = [UIFont boldSystemFontOfSize:14];CGSize textSize = [text sizeWithFont:fontconstrainedToSize:CGSizeMake(280, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, textSize.width + 12, textSize.height + 12)];textLabel.backgroundColor = [UIColor clearColor];textLabel.textColor = [UIColor whiteColor];textLabel.textAlignment = UITextAlignmentCenter;textLabel.font = font;textLabel.text = text;textLabel.numberOfLines = 0;contentView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, textLabel.frame.size.width, textLabel.frame.size.height)];contentView.layer.cornerRadius = 5.0f;contentView.layer.borderWidth = 1.0f;contentView.layer.borderColor = [[UIColor grayColor] colorWithAlphaComponent:0.5].CGColor;contentView.backgroundColor = [UIColor colorWithRed:0.2fgreen:0.2fblue:0.2falpha:0.75f];[contentView addSubview:textLabel];contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;[contentView addTarget:selfaction:@selector(toastTaped:)forControlEvents:UIControlEventTouchDown];contentView.alpha = 0.0f;duration = 3;[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(deviceOrientationDidChanged:)name:UIDeviceOrientationDidChangeNotificationobject:[UIDevice currentDevice]];}return self;
}- (void)deviceOrientationDidChanged:(NSNotification *)notify_{[self hideAnimation];
}-(void)dismissToast{[contentView removeFromSuperview];
}-(void)toastTaped:(UIButton *)sender_{[self hideAnimation];
}- (void)setDuration:(CGFloat) duration_{duration = duration_;
}-(void)showAnimation{[UIView beginAnimations:@"show" context:NULL];[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];[UIView setAnimationDuration:0.3];contentView.alpha = 1.0f;[UIView commitAnimations];
}-(void)hideAnimation{[UIView beginAnimations:@"hide" context:NULL];[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];[UIView setAnimationDelegate:self];[UIView setAnimationDidStopSelector:@selector(dismissToast)];[UIView setAnimationDuration:0.3];contentView.alpha = 0.0f;[UIView commitAnimations];
}- (void)show{UIWindow *window = [UIApplication sharedApplication].keyWindow;contentView.center = window.center;[window addSubview:contentView];[self showAnimation];[self performSelector:@selector(hideAnimation) withObject:nil afterDelay:duration];
}- (void)showFromTopOffset:(CGFloat) top_{UIWindow *window = [UIApplication sharedApplication].keyWindow;contentView.center = CGPointMake(window.center.x, top_ + contentView.frame.size.height/2);[window addSubview:contentView];[self showAnimation];[self performSelector:@selector(hideAnimation) withObject:nil afterDelay:duration];
}- (void)showFromBottomOffset:(CGFloat) bottom_{UIWindow *window = [UIApplication sharedApplication].keyWindow;contentView.center = CGPointMake(window.center.x, window.frame.size.height-(bottom_ + contentView.frame.size.height/2));[window addSubview:contentView];[self showAnimation];[self performSelector:@selector(hideAnimation) withObject:nil afterDelay:duration];
}+ (void)showWithText:(NSString *)text_{[OMGToast showWithText:text_ duration:3];
}+ (void)showWithText:(NSString *)text_duration:(CGFloat)duration_{OMGToast *toast = [[OMGToast alloc] initWithText:text_];[toast setDuration:duration_];[toast show];
}+ (void)showWithText:(NSString *)text_topOffset:(CGFloat)topOffset_{[OMGToast showWithText:text_ topOffset:topOffset_ duration:3];
}+ (void)showWithText:(NSString *)text_topOffset:(CGFloat)topOffset_duration:(CGFloat)duration_{OMGToast *toast = [[OMGToast alloc] initWithText:text_];[toast setDuration:duration_];[toast showFromTopOffset:topOffset_];
}+ (void)showWithText:(NSString *)text_bottomOffset:(CGFloat)bottomOffset_{[OMGToast showWithText:text_ bottomOffset:bottomOffset_ duration:3];
}+ (void)showWithText:(NSString *)text_bottomOffset:(CGFloat)bottomOffset_duration:(CGFloat)duration_{OMGToast *toast = [[OMGToast alloc] initWithText:text_];[toast setDuration:duration_];[toast showFromBottomOffset:bottomOffset_];
}@end
可以这样来使用
[OMGToast showWithText:@"中间显示" duration:5];
[OMGToast showWithText:@"距离上方50像素" topOffset:50 duration:5];
[OMGToast showWithText:@"文字很多的时候,我就会自动折行,最大宽度280像素" topOffset:100 duration:5];
[OMGToast showWithText:@"加入\\n也可以\n显示\n多\n行" topOffset:300 duration:5];
[OMGToast showWithText:@"距离下方3像素" bottomOffset:3 duration:5];