您好,登錄后才能下訂單哦!
h:
#import <UIKit/UIKit.h>
#define RGB(a, b, c) [UIColor colorWithRed:(a / 255.0f) green:(b / 255.0f) blue:(c / 255.0f) alpha:1.0f]
#define RGBA(a, b, c, d) [UIColor colorWithRed:(a / 255.0f) green:(b / 255.0f) blue:(c / 255.0f) alpha:d]
@interface MyToast : UIView
+ (void)showWithText:(NSString *)text:(int)toastY;
+ (void)showWithImage:(UIImage *)p_w_picpath;
@end
m:
#import "MyToast.h"
#import <QuartzCore/QuartzCore.h>
@implementation MyToast
- (void)__show {
[UIView beginAnimations:@"show" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.2f];
[UIView setAnimationDidStopSelector:@selector(__animationDidStop:__finished:__context:)];
self.alpha = 1.0f;
[UIView commitAnimations];
}
- (void)__hide {
[self performSelectorOnMainThread:@selector(__hideThread) withObject:nil waitUntilDone:NO];
}
- (void)__hideThread {
[UIView beginAnimations:@"hide" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.8f];
[UIView setAnimationDidStopSelector:@selector(__animationDidStop:__finished:__context:)];
self.alpha = 0.0f;
[UIView commitAnimations];
}
- (void)__animationDidStop:(NSString *)animationID __finished:(NSNumber *)finished __context:(void *)context {
if ([animationID isEqualToString:@"hide"]) {
[self removeFromSuperview];
self = nil;
}
else if ([animationID isEqualToString:@"show"]) {
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(__hide) userInfo:nil repeats:NO];
}
}
+ (MyToast *)__createWithText:(NSString *)text:(int)toastY {
float screenWidth = [UIScreen mainScreen].bounds.size.width;
// float screenHeight = [UIScreen mainScreen].bounds.size.height;
float x = 10.0f;
float width = screenWidth - x * 2.0f;
UILabel *textLabel = [[UILabel alloc] init];
textLabel.backgroundColor = [UIColor clearColor];
textLabel.textAlignment = UITextAlignmentCenter;
textLabel.font = [UIFont systemFontOfSize:14];
textLabel.textColor = RGB(255, 255, 255);
textLabel.numberOfLines = 0;
textLabel.lineBreakMode = UILineBreakModeCharacterWrap;
CGSize sz = [text sizeWithFont:textLabel.font
constrainedToSize:CGSizeMake(width - 20.0f, 9999.0f)
lineBreakMode:textLabel.lineBreakMode];
CGRect tmpRect;
tmpRect.size.width = width;
tmpRect.size.height = MAX(sz.height + 20.0f, 38.0f);
tmpRect.origin.x = floor((screenWidth - width) / 2.0f);
tmpRect.origin.y =toastY;// floor(screenHeight - tmpRect.size.height - 15.0f);
MyToast *toast = [[MyToast alloc] initWithFrame:tmpRect];
toast.backgroundColor = RGBA(0, 0, 0, 0.8f);
CALayer *layer = toast.layer;
layer.masksToBounds = YES;
layer.cornerRadius = 5.0f;
textLabel.text = text;
tmpRect.origin.x = floor((toast.frame.size.width - sz.width) / 2.0f);
tmpRect.origin.y = floor((toast.frame.size.height - sz.height) / 2.0f);
tmpRect.size = sz;
textLabel.frame = tmpRect;
[toast addSubview:textLabel];
[textLabel release];
toast.alpha = 0.0f;
return toast;
}
+ (MyToast *)__createWithImage:(UIImage *)p_w_picpath {
float screenWidth = [UIScreen mainScreen].bounds.size.width;
float screenHeight = [UIScreen mainScreen].bounds.size.height;
float x = 10.0f;
float width = screenWidth - x * 2.0f;
UIImageView *p_w_picpathView = [[UIImageView alloc] initWithImage:p_w_picpath];
CGSize sz = p_w_picpathView.frame.size;
CGRect tmpRect;
tmpRect.size.width = width;
tmpRect.size.height = MAX(sz.height + 20.0f, 38.0f);
tmpRect.origin.x = floor((screenWidth - width) / 2.0f);
tmpRect.origin.y = floor(screenHeight - tmpRect.size.height - 15.0f);
MyToast *toast = [[MyToast alloc] initWithFrame:tmpRect];
toast.backgroundColor = RGBA(0, 0, 0, 0.8f);
CALayer *layer = toast.layer;
layer.masksToBounds = YES;
layer.cornerRadius = 5.0f;
tmpRect.origin.x = floor((toast.frame.size.width - sz.width) / 2.0f);
tmpRect.origin.y = floor((toast.frame.size.height - sz.height) / 2.0f);
tmpRect.size = sz;
p_w_picpathView.frame = tmpRect;
[toast addSubview:p_w_picpathView];
[p_w_picpathView release];
toast.alpha = 0.0f;
return toast;
}
/**
* Show toast with text in application window
* @param text Text to print in toast window
*/
+ (void)showWithText:(NSString *)text:(int)toastY {
MyToast *toast = [MyToast __createWithText:text:toastY];
UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow];
[mainWindow addSubview:toast];
[toast release];
[toast __show];
}
/**
* Show toast with p_w_picpath in application window
* @param p_w_picpath Image to show in toast window
*/
+ (void)showWithImage:(UIImage *)p_w_picpath {
MyToast *toast = [MyToast __createWithImage:p_w_picpath];
UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow];
[mainWindow addSubview:toast];
[toast release];
[toast __show];
}@end
引用:
#import "MyToast.h"
[MyToast showWithText:@"這里還什么都沒有哦 親!":380];
數字是顯示的Y坐標。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。