2022-09-21ios 实现自定义toast功能

1> h文件

@interface MyAlert : UIView
-(id)init;
- (void) setMessageText:(NSString *)message;
@end
 
@interface MyAlertCenter : NSObject{
    
    MyAlert *myAlertView;//alertView
    BOOL active;//当前是否在用
    
    
}
 
+ (MyAlertCenter *) defaultCenter;//单例 生成alert控制器
- (void) postAlertWithMessage:(NSString*)message;//弹出文字
 
@end

2>.m文件


#import "MyAlert.h"
 
 
@implementation MyAlertCenter
 
+ (MyAlertCenter *) defaultCenter{
    static MyAlertCenter *defaultCenter;
    if (!defaultCenter) {
        defaultCenter=[[MyAlertCenter alloc]init];
    }
    return defaultCenter;
}
 
- (id) init{
    if(!(self=[super init])) return nil;
    
    myAlertView = [[MyAlert alloc] init];
    myAlertView.alpha = 0.0f;
    active = NO;
    
    [[UIApplication sharedApplication].keyWindow addSubview:myAlertView];
    
    return self;
}
 
- (void) postAlertWithMessage:(NSString*)message{
    //判断当前是否在使用中
    if (!active) {
        [self showAlerts:message];
    }
}
 
- (void) showAlerts:(NSString *) str {
    
    //开始使用,设置当前为使用状态
    active = YES;
    myAlertView.alpha = 0;
    [[UIApplication sharedApplication].keyWindow addSubview:myAlertView];
    [myAlertView setMessageText:str];
    myAlertView.center = [UIApplication sharedApplication].keyWindow.center;
    
    //设置动画
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationStep2)];
    myAlertView.alpha = 0.8;
    [UIView commitAnimations];
}
 
- (void) animationStep2{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelay:1.0];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationStep3)];
    myAlertView.alpha = 0;
    [UIView commitAnimations];
}
 
 
- (void) animationStep3{
    
    [myAlertView removeFromSuperview];
    active=NO;
    
}
@end
 
 
 
 
@implementation MyAlert
 
CGRect messageRect;
NSString *text;
 
-(id)init{
    
    self=[super initWithFrame:CGRectMake(0, 0, 100, 10)];
    if (self) {
        messageRect =CGRectInset(self.bounds, 10, 10);
    }
    return self;
}
-(void)setMessageText:(NSString *)message{
    
    text=message;
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};
    
    CGSize s=[text boundingRectWithSize:CGSizeMake(100, 0) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    
    self.bounds = CGRectMake(0, 0, s.width+40, s.height+15+15);
    
    messageRect.size = s;
    messageRect.size.height += 5;
    messageRect.origin.x = 20;
    messageRect.origin.y = 15;
    
    [self setNeedsLayout];
    [self setNeedsDisplay];
}
 
-(void)drawRect:(CGRect)rect
{
    NSDictionary* attrs =@{NSForegroundColorAttributeName:[UIColor whiteColor]
                           ,NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:18]
                           };
    [text drawInRect:messageRect withAttributes:attrs]; //给文本限制个矩形边界,防止矩形拉伸;
}
 
 
@end

3>调用方法

 [[MyAlertCenter defaultCenter] postAlertWithMessage:errorModel.errorMessage];

调用时需要导入头文件

#import "MyAlert.h"
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容