温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iphone下类似anroid的toast类 吐司提示

发布时间:2020-06-28 16:59:43 来源:网络 阅读:527 作者:yvbang 栏目:开发技术

 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坐标。

 

 

 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI