ios 类似于android Toast
#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}iToastType;@class iToastSettings;@interface iToast : NSObject {iToastSettings *_settings;NSInteger offsetLeft;NSInteger offsetTop;NSTimer *timer;UIView *view;NSString *text;}- (void) show;- (iToast *) setDuration:(NSInteger ) duration;- (iToast *) setGravity:(iToastGravity) gravityoffsetLeft:(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 = ) {text = ;}return self;}- (void) show{iToastSettings *theSettings = _settings;if (!theSettings) {theSettings = ;}UIFont *font = ;CGSize textSize = ;UILabel *label = [ initWithFrame:CGRectMake(0, 0, textSize.width + 5, textSize.height + 5)];label.backgroundColor = ;label.textColor = ;label.font = font;label.text = text;label.numberOfLines = 0;label.shadowColor = ;label.shadowOffset = CGSizeMake(1, 1);UIButton *v = ;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.backgroundColor = ;v.layer.cornerRadius = 5;UIWindow *window = [[ windows] objectAtIndex:0];CGPoint point = CGPointMake(window.frame.size.width/2, window.frame.size.height/2);if (theSettings.gravity == iToastGravityTop) {point = CGPointMake(window.frame.size.width / 2, 45);}else if (theSettings.gravity == iToastGravityBottom) {point = CGPointMake(window.frame.size.width / 2, window.frame.size.height - 45);}else if (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);v.center = point;NSTimer *timer1 = ;[ addTimer:timer1 forMode:NSDefaultRunLoopMode];;view = ;;}- (void) hideToast:(NSTimer*)theTimer{;view.alpha = 0;;NSTimer *timer2 = ;[ addTimer:timer2 forMode:NSDefaultRunLoopMode];}- (void) removeToast:(NSTimer*)theTimer{;}+ (iToast *) makeText:(NSString *) _text{iToast *toast = [[ initWithText:_text] autorelease];return toast;}- (iToast *) setDuration:(NSInteger ) duration{.duration = duration;return self;}- (iToast *) setGravity:(iToastGravity) gravityoffsetLeft:(NSInteger) leftoffsetTop:(NSInteger) top{.gravity = gravity;offsetLeft = left;offsetTop = top;return self;}- (iToast *) setGravity:(iToastGravity) gravity{.gravity = gravity;return self;}- (iToast *) setPostion:(CGPoint) _position{.postition = CGPointMake(_position.x, _position.y);return self;}-(iToastSettings *) theSettings{if (!_settings) {_settings = [ copy];}return _settings;}@end@implementation iToastSettings@synthesize duration;@synthesize gravity;@synthesize postition;@synthesize images;- (void) setImage:(UIImage *) img forType:(iToastType) type{if (!images) {images = [ initWithCapacity:4];}if (img) {NSString *key = ;;}}+ (iToastSettings *) getSharedSettings{if (!sharedSettings) {sharedSettings = ;sharedSettings.gravity = iToastGravityCenter;sharedSettings.duration = iToastDurationShort;}return sharedSettings;}- (id) copyWithZone:(NSZone *)zone{iToastSettings *copy = ;copy.gravity = self.gravity;copy.duration = self.duration;copy.postition = self.postition;NSArray *keys = ;for (NSString *key in keys){ forType:];}return copy;}@end
//调用
[ show];
页:
[1]