IOS Toast
来源:互联网 发布:电脑容易被入侵的端口 编辑:程序博客网 时间:2024/06/14 05:07
iOS的风格和Apple其他产品一样,简单而粗暴。没有给人其他选择的余地,让你又爱又恨。同样的,Apple对待iOS平台的开发人员和对待大众消费者一样,也不给你留余地。UIAlertView就是一个鲜明标志。功能简单,甚至单一,定制性差,消耗资源。在资源紧张的设备上,UIAlertView的动画效果都会稍微卡一下,很是别扭。
这时还是很希望在iOS上有一个Android风格的Toast控件。
终于http://code.google.com/p/toast-notifications-ios/ 发布了这样的控件。在这里和大家分享一下。
我制作了一个demo,使用起来感觉效果还是很不错的。
这个类的接口设计如下.h:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
typedef enum iToastGravity {
iToastGravityTop = 1000001,
iToastGravityBottom,
iToastGravityCenter
}iToastGravity;
enum iToastDuration {
iToastDurationLong = 10000,
iToastDurationShort = 1000,
iToastDurationNormal = 3000
}iToastDuration;
typedef enum iToastType {
iToastTypeInfo = -100000,
iToastTypeNotice,
iToastTypeWarning,
iToastTypeError,
iToastTypeNone // For internal use only (to force no image)
}iToastType;
@class iToastSettings;
@interface iToast : NSObject {
iToastSettings *_settings;
NSInteger offsetLeft;
NSInteger offsetTop;
NSTimer *timer;
UIView *view;
NSString *text;
}
- (void) show;
- (void) show:(iToastType) type;
- (iToast *) setDuration:(NSInteger ) duration;
- (iToast *) setGravity:(iToastGravity) gravity
offsetLeft:(NSInteger) left
offsetTop:(NSInteger) top;
- (iToast *) setGravity:(iToastGravity) gravity;
- (iToast *) setPostion:(CGPoint) position;
+ (iToast *) makeText:(NSString *) text;
-(iToastSettings *) theSettings;
@end
@interface iToastSettings : NSObject<NSCopying>{
NSInteger duration;
iToastGravity gravity;
CGPoint postition;
iToastType toastType;
NSDictionary *images;
BOOL positionIsSet;
}
@property(assign) NSInteger duration;
@property(assign) iToastGravity gravity;
@property(assign) CGPoint postition;
@property(readonly) NSDictionary *images;
- (void) setImage:(UIImage *)img forType:(iToastType) type;
+ (iToastSettings *) getSharedSettings;
@end
===============================================
.m#import "iToast.h"
#import <QuartzCore/QuartzCore.h>
static iToastSettings *sharedSettings =nil;
@interface iToast(private)
- (iToast *) settings;
@end
@implementation iToast
- (id) initWithText:(NSString *) tex{
if (self = [superinit]) {
text = [tex copy];
}
return self;
}
- (void) show{
[selfshow:iToastTypeNone];
}
- (void) show:(iToastType) type{
iToastSettings *theSettings =_settings;
if (!theSettings) {
theSettings = [iToastSettingsgetSharedSettings];
}
UIImage *image = [theSettings.imagesvalueForKey:[NSString stringWithFormat:@"%i", type]];
UIFont *font = [UIFontsystemFontOfSize:16];
CGSize textSize = [textsizeWithFont:font constrainedToSize:CGSizeMake(280,60)];
UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(0,0, textSize.width + 5, textSize.height + 5)];
label.backgroundColor = [UIColorclearColor];
label.textColor = [UIColorwhiteColor];
label.font = font;
label.text = text;
label.numberOfLines =0;
label.shadowColor = [UIColordarkGrayColor];
label.shadowOffset = CGSizeMake(1, 1);
UIButton *v = [UIButtonbuttonWithType:UIButtonTypeCustom];
if (image) {
v.frame = CGRectMake(0, 0, image.size.width + textSize.width +15, MAX(textSize.height, image.size.height) +10);
label.center = CGPointMake(image.size.width +10 + (v.frame.size.width - image.size.width -10) / 2, v.frame.size.height /2);
} else {
v.frame = CGRectMake(0, 0, textSize.width +10, textSize.height + 10);
label.center =CGPointMake(v.frame.size.width /2, v.frame.size.height /2);
}
[v addSubview:label];
[label release];
if (image) {
UIImageView *imageView = [[UIImageViewalloc] initWithImage:image];
imageView.frame =CGRectMake(5, (v.frame.size.height - image.size.height)/2, image.size.width, image.size.height);
[v addSubview:imageView];
[imageView release];
}
v.backgroundColor = [UIColorcolorWithRed:0green:0 blue:0 alpha:0.7];
v.layer.cornerRadius =5;
UIWindow *window = [[[UIApplicationsharedApplication] windows] objectAtIndex:0];
CGPoint point = CGPointMake(window.frame.size.width/2, window.frame.size.height/2);
// Set correct orientation/location regarding device orientation
UIInterfaceOrientation orientation = (UIInterfaceOrientation)[[UIApplicationsharedApplication] statusBarOrientation];
switch (orientation) {
caseUIDeviceOrientationPortrait:
{
if (theSettings.gravity ==iToastGravityTop) {
point = CGPointMake(window.frame.size.width /2, 45);
} elseif (theSettings.gravity == iToastGravityBottom) {
point = CGPointMake(window.frame.size.width /2, window.frame.size.height -45);
} elseif (theSettings.gravity == iToastGravityCenter) {
point = CGPointMake(window.frame.size.width/2, window.frame.size.height/2);
} else {
point = theSettings.postition;
}
point = CGPointMake(point.x +offsetLeft, point.y + offsetTop);
break;
}
caseUIDeviceOrientationPortraitUpsideDown:
{
v.transform =CGAffineTransformMakeRotation(M_PI);
float width = window.frame.size.width;
float height = window.frame.size.height;
if (theSettings.gravity ==iToastGravityTop) {
point = CGPointMake(width / 2, height - 45);
} elseif (theSettings.gravity == iToastGravityBottom) {
point = CGPointMake(width / 2, 45);
} elseif (theSettings.gravity == iToastGravityCenter) {
point = CGPointMake(width/2, height/2);
} else {
// TODO : handle this case
point = theSettings.postition;
}
point = CGPointMake(point.x -offsetLeft, point.y - offsetTop);
break;
}
caseUIDeviceOrientationLandscapeLeft:
{
v.transform =CGAffineTransformMakeRotation(M_PI/2);//rotation in radians
if (theSettings.gravity ==iToastGravityTop) {
point = CGPointMake(window.frame.size.width -45, window.frame.size.height /2);
} elseif (theSettings.gravity == iToastGravityBottom) {
point = CGPointMake(45,window.frame.size.height /2);
} elseif (theSettings.gravity == iToastGravityCenter) {
point = CGPointMake(window.frame.size.width/2, window.frame.size.height/2);
} else {
// TODO : handle this case
point = theSettings.postition;
}
point = CGPointMake(point.x -offsetTop, point.y - offsetLeft);
break;
}
caseUIDeviceOrientationLandscapeRight:
{
v.transform =CGAffineTransformMakeRotation(-M_PI/2);
if (theSettings.gravity ==iToastGravityTop) {
point = CGPointMake(45, window.frame.size.height /2);
} elseif (theSettings.gravity == iToastGravityBottom) {
point = CGPointMake(window.frame.size.width -45, window.frame.size.height/2);
} elseif (theSettings.gravity == iToastGravityCenter) {
point = CGPointMake(window.frame.size.width/2, window.frame.size.height/2);
} else {
// TODO : handle this case
point = theSettings.postition;
}
point = CGPointMake(point.x +offsetTop, point.y + offsetLeft);
break;
}
default:
break;
}
v.center = point;
NSTimer *timer1 = [NSTimertimerWithTimeInterval:((float)theSettings.duration)/1000
target:selfselector:@selector(hideToast:)
userInfo:nil repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:timer1forMode:NSDefaultRunLoopMode];
[window addSubview:v];
view = [v retain];
[v addTarget:selfaction:@selector(hideToast:)forControlEvents:UIControlEventTouchDown];
}
- (void) hideToast:(NSTimer*)theTimer{
[UIViewbeginAnimations:nilcontext:NULL];
view.alpha =0;
[UIViewcommitAnimations];
NSTimer *timer2 = [NSTimertimerWithTimeInterval:500
target:selfselector:@selector(hideToast:)
userInfo:nil repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:timer2forMode:NSDefaultRunLoopMode];
}
- (void) removeToast:(NSTimer*)theTimer{
[viewremoveFromSuperview];
}
+ (iToast *) makeText:(NSString *) _text{
iToast *toast = [[[iToastalloc] initWithText:_text] autorelease];
return toast;
}
- (iToast *) setDuration:(NSInteger ) duration{
[self theSettings].duration = duration;
return self;
}
- (iToast *) setGravity:(iToastGravity) gravity
offsetLeft:(NSInteger) left
offsetTop:(NSInteger) top{
[self theSettings].gravity = gravity;
offsetLeft = left;
offsetTop = top;
return self;
}
- (iToast *) setGravity:(iToastGravity) gravity{
[self theSettings].gravity = gravity;
return self;
}
- (iToast *) setPostion:(CGPoint) _position{
[self theSettings].postition =CGPointMake(_position.x, _position.y);
return self;
}
-(iToastSettings *) theSettings{
if (!_settings) {
_settings = [[iToastSettingsgetSharedSettings] copy];
}
return_settings;
}
@end
@implementation iToastSettings
@synthesize duration;
@synthesize gravity;
@synthesize postition;
@synthesize images;
- (void) setImage:(UIImage *) img forType:(iToastType) type{
if (type == iToastTypeNone) {
// This should not be used, internal use only (to force no image)
return;
}
if (!images) {
images = [[NSMutableDictionaryalloc] initWithCapacity:4];
}
if (img) {
NSString *key = [NSStringstringWithFormat:@"%i", type];
[images setValue:img forKey:key];
}
}
+ (iToastSettings *) getSharedSettings{
if (!sharedSettings) {
sharedSettings = [iToastSettingsnew];
sharedSettings.gravity =iToastGravityCenter;
sharedSettings.duration =iToastDurationShort;
}
returnsharedSettings;
}
- (id) copyWithZone:(NSZone *)zone{
iToastSettings *copy = [iToastSettingsnew];
copy.gravity =self.gravity;
copy.duration =self.duration;
copy.postition =self.postition;
NSArray *keys = [self.imagesallKeys];
for (NSString *keyin keys){
[copy setImage:[imagesvalueForKey:key] forType:[key intValue]];
}
return copy;
}
@end
从接口上看,可以设置风格和显示时间,显示位置等,但是从实现代码上看是图片显示只是预留的接口,尚未实现。其显示的位置在设备旋转时也没有进行处理,没有进行横屏等其他方向的显示控制。
简单的调用
[[iToast makeText:NSLocalizedString(@"The activity has been successfully saved.",@"")] show];
设置显示位置
[[[iToast makeText:NSLocalizedString(@"The activity has been successfully saved.", @"")] setGravity:iToastGravityBottom] show];
设置显示位置和显示时长类型
[[[[iToast makeText:NSLocalizedString(@"Something to display a very long time",@"")] setGravity:iToastGravityBottom] setDuration:iToastDurationLong] show];
- IOS Toast
- IOS Toast
- 开源 toast-notifications-ios
- ios 类似于android Toast
- ios 类似于android Toast
- iOS 提示 toast
- iOS 提示 Toast
- ios实现toast
- ios版toast
- ios 类似android的toast
- iOS 仿Android Toast效果
- Toast
- Toast
- Toast
- Toast
- Toast
- Toast
- toast
- 【javascript】php对json格式数据处理的总结
- VMware Linux安装Oracle( 下 )
- Facebook称日前新西兰广告实为恶意软件
- android--手机震动--Vibrator
- jqGrid基础学习:5jqGrid中事件的使用
- IOS Toast
- <context-param>与<init-param>的区别与作用
- linux驱动的入口函数module_init的加载和释放
- 王爽 汇编语言 实验七
- C++标准库的学习笔记(开篇)
- socket简述
- PopupWindow(一)
- 嵌入式研发人员的核心竞争力浅谈 .
- AlterDialog的使用方法