首页 > 代码库 > iOS开发:Toast for iPhone
iOS开发:Toast for iPhone
iOS开发:Toast for iPhone
分享一个我写的类似于android的toast的提示框
主要特点:
1,支持屏幕Y轴任意位置显示,设置距离顶/底端距离
2,支持多行文本
3,支持设置等待时间
4,支持点击隐藏,屏幕旋转时自动隐藏,淡入淡出
5,无需初始化,类方法调用
效果图:
全部代码如下,使用时需要添加QuartzCore.framework,希望能给大家带来方便。
- #import <Foundation/Foundation.h>
- #define DEFAULT_DISPLAY_DURATION 2.0f
- @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) topOffset
- duration:(CGFloat) duration_;
- + (void)showWithText:(NSString *) text_
- bottomOffset:(CGFloat) bottomOffset_;
- + (void)showWithText:(NSString *) text_
- bottomOffset:(CGFloat) bottomOffset_
- duration:(CGFloat) duration_;
- @end
- #import "OMGToast.h"
- #import <QuartzCore/QuartzCore.h>
- @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:self
- name:UIDeviceOrientationDidChangeNotification
- object:[UIDevice currentDevice]];
- [contentView release],contentView = nil;
- [text release],text = nil;
- [super dealloc];
- }
- - (id)initWithText:(NSString *)text_{
- if (self = [super init]) {
- text = [text_ copy];
- UIFont *font = [UIFont boldSystemFontOfSize:14];
- CGSize textSize = [text sizeWithFont:font
- constrainedToSize: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.2f
- green:0.2f
- blue:0.2f
- alpha:0.75f];
- [contentView addSubview:textLabel];
- contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- [contentView addTarget:self
- action:@selector(toastTaped:)
- forControlEvents:UIControlEventTouchDown];
- contentView.alpha = 0.0f;
- [textLabel release];
- duration = DEFAULT_DISPLAY_DURATION;
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(deviceOrientationDidChanged:)
- name:UIDeviceOrientationDidChangeNotification
- object:[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:DEFAULT_DISPLAY_DURATION];
- }
- + (void)showWithText:(NSString *)text_
- duration:(CGFloat)duration_{
- OMGToast *toast = [[[OMGToast alloc] initWithText:text_] autorelease];
- [toast setDuration:duration_];
- [toast show];
- }
- + (void)showWithText:(NSString *)text_
- topOffset:(CGFloat)topOffset_{
- [OMGToast showWithText:text_ topOffset:topOffset_ duration:DEFAULT_DISPLAY_DURATION];
- }
- + (void)showWithText:(NSString *)text_
- topOffset:(CGFloat)topOffset_
- duration:(CGFloat)duration_{
- OMGToast *toast = [[[OMGToast alloc] initWithText:text_] autorelease];
- [toast setDuration:duration_];
- [toast showFromTopOffset:topOffset_];
- }
- + (void)showWithText:(NSString *)text_
- bottomOffset:(CGFloat)bottomOffset_{
- [OMGToast showWithText:text_ bottomOffset:bottomOffset_ duration:DEFAULT_DISPLAY_DURATION];
- }
- + (void)showWithText:(NSString *)text_
- bottomOffset:(CGFloat)bottomOffset_
- duration:(CGFloat)duration_{
- OMGToast *toast = [[[OMGToast alloc] initWithText:text_] autorelease];
- [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];
iOS开发:Toast for iPhone
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。