iOS开发:Toast for iPhone 实例分享
2013-08-02 14:00
537 查看
分享一个我写的类似于android的toast的提示框
主要特点:
1,支持屏幕Y轴任意位置显示,设置距离顶/底端距离
2,支持多行文本
3,支持设置等待时间
4,支持点击隐藏,屏幕旋转时自动隐藏,淡入淡出
5,无需初始化,类方法调用
效果图:
全部代码如下,使用时需要添加QuartzCore.framework,希望能给大家带来方便。
[cpp] view
plaincopyprint?
#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
[cpp] view
plaincopyprint?
#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
可以这样来使用
[cpp] view
plaincopyprint?
[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];
转自:http://blog.csdn.net/iBright/article/details/7078436
主要特点:
1,支持屏幕Y轴任意位置显示,设置距离顶/底端距离
2,支持多行文本
3,支持设置等待时间
4,支持点击隐藏,屏幕旋转时自动隐藏,淡入淡出
5,无需初始化,类方法调用
效果图:
全部代码如下,使用时需要添加QuartzCore.framework,希望能给大家带来方便。
[cpp] view
plaincopyprint?
#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
[cpp] view
plaincopyprint?
#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
可以这样来使用
[cpp] view
plaincopyprint?
[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];
转自:http://blog.csdn.net/iBright/article/details/7078436
相关文章推荐
- iOS开发:Toast for iPhone 实例分享
- iOS开发:Toast for iPhone 实例分享
- iOS开发:Toast for iPhone
- demo for iphone苹果官方例子ios开发
- iPhone游戏编程实例:分享成功游戏开发人员的锦囊妙计
- iOS开发中Can't find keyplane that supports type 5 for keyboard iPhone-Portrait-PhonePad的解决方法
- iphone Dev 开发实例9:Create Grid Layout Using UICollectionView in iOS 6
- ios 网络电话开发 (Voip Client For iPhone )
- IOS开发教程之put上传文件的服务器的配置及实例分享-备用
- iOS开发人员必备App开发工具 ifunbox 支持iPhone, iPad和iPod Touch的文件及应用管理神器 使用实例
- iOS开发中Can't find keyplane that supports type 5 for keyboard iPhone-Portrait-PhonePad的解决方法
- ios--Toast for iPhone(实例分享)
- demo for iphone苹果官方例子ios开发
- iOS开发-iPhoneX的适配-iPhoneX屏幕适配分享
- 【无限互联】iOS开发视频教程— 2.6 iPhone开发之if条件和for循环语句
- iOS学习资料分享 -- 苹果官方iPhone应用高级开发课程(16集)
- iOS开发中的几个手势操作实例分享
- iOS学习资料分享 -- 苹果官方iPhone应用高级开发课程(16集)
- iOS学习资料分享 -- 苹果官方iPhone应用高级开发课程(16集)
- 在开发iOS程序时,有时候需要将时间格式调整成自己希望的格式,这个时候我们可以用NSDateFormatter类来处理。 例如: //实例化一个NSDateFormatter对象 NSDateFor