123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- //
- // iToast.m
- // iToast
- //
- // Created by Diallo Mamadou Bobo on 2/10/11.
- // Copyright 2011 __MyCompanyName__. All rights reserved.
- //
- #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 = [super init]) {
- text = [tex copy];
- }
-
- return self;
- }
- - (void) show{
-
- iToastSettings *theSettings = _settings;
-
- if (!theSettings) {
- theSettings = [iToastSettings getSharedSettings];
- }
-
- UIFont *font = [UIFont systemFontOfSize:14];
-
- CGSize textSize = [text boundingRectWithSize:CGSizeMake(280, 60) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil].size;
- textSize.height += 10;
- textSize.width += 10;
-
- UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, textSize.width + 5, textSize.height + 5)];
- label.backgroundColor = [UIColor clearColor];
- label.textAlignment = NSTextAlignmentCenter;
- label.textColor = [UIColor whiteColor];
- label.font = font;
- label.text = text;
- label.alpha = 0 ;
- label.numberOfLines = 0;
-
- UIButton *v = [UIButton buttonWithType:UIButtonTypeCustom];
- v.frame = CGRectMake(0, 0, textSize.width + 200, textSize.height + 200);
- label.center = CGPointMake(v.frame.size.width / 2, v.frame.size.height / 2);
- [v addSubview:label];
-
- v.backgroundColor = [UIColor darkGrayColor]; // 背景不要透明
- v.layer.cornerRadius = 5;
- v.alpha = 0 ;
-
- UIWindow *window = [[[UIApplication sharedApplication] 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 = [NSTimer timerWithTimeInterval:((float)theSettings.duration)/1000
- target:self selector:@selector(hideToast:)
- userInfo:nil repeats:NO];
- [[NSRunLoop mainRunLoop] addTimer:timer1 forMode:NSDefaultRunLoopMode];
-
- [window addSubview:v];
-
- [UIView beginAnimations:nil context:NULL];
- CGRect _frame= v.frame;
- float _w = textSize.width + 10;
- float _h = textSize.height + 10;
- float _x = _frame.origin.x + (_frame.size.width - _w)/2;
- float _y = _frame.origin.y + (_frame.size.height - _h)/2;
-
- [label setFrame:CGRectMake(2, 2, textSize.width + 5, textSize.height + 5)];
- label.alpha = 1 ;
- [v setFrame:CGRectMake(_x,_y,_w,_h)];
- v.alpha = 1;
- [UIView commitAnimations];
-
-
-
- view = v;
-
- [v addTarget:self action:@selector(hideToast:) forControlEvents:UIControlEventTouchDown];
- }
- - (void) removeToast{
- [view removeFromSuperview];
- }
- - (void) hideToast:(NSTimer*)theTimer{
- [UIView beginAnimations:nil context:NULL];
- view.alpha = 0;
- [UIView commitAnimations];
-
- [self removeToast];
- }
- + (iToast *) makeText:(NSString *) _text{
- iToast *toast = [[iToast alloc] initWithText:_text];
-
- 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 = [[iToastSettings getSharedSettings] copy];
- }
-
- return _settings;
- }
- @end
- @implementation iToastSettings
- @synthesize duration;
- @synthesize gravity;
- @synthesize postition;
- @synthesize images;
- - (void) setImage:(UIImage *) img forType:(iToastType) type{
- if (!images) {
- images = [[NSMutableDictionary alloc] initWithCapacity:4];
- }
-
- if (img) {
- NSString *key = [NSString stringWithFormat:@"%i", type];
- [images setValue:img forKey:key];
- }
- }
- + (iToastSettings *) getSharedSettings{
- if (!sharedSettings) {
- sharedSettings = [iToastSettings new];
- sharedSettings.gravity = iToastGravityCenter;
- sharedSettings.duration = 1500; //增加显示时间,是能够截图
- }
-
- return sharedSettings;
-
- }
- - (id) copyWithZone:(NSZone *)zone{
- iToastSettings *copy = [iToastSettings new];
- copy.gravity = self.gravity;
- copy.duration = self.duration;
- copy.postition = self.postition;
-
- NSArray *keys = [self.images allKeys];
-
- for (NSString *key in keys){
- [copy setImage:[images valueForKey:key] forType:(iToastType)[key intValue]];
- }
-
- return copy;
- }
- @end
|