溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2020-06-28 16:59:43 來(lái)源:網(wǎng)絡(luò) 閱讀:523 作者:yvbang 欄目:開發(fā)技術(shù)

 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:@"這里還什么都沒(méi)有哦 親!":380];

 

 

數(shù)字是顯示的Y坐標(biāo)。

 

 

 

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI