iOS 弹框提示 2秒隐藏

看了网上很多弹框提示也学着自己写一个封装

写的不好,请多多指点!


调用:[GXW_BouncedWithdrawalView BouncedRemindTitle:@"测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试"];

注意:这里没有做文字为空的判断,所以需要先判断,再调用!

直接上代码!!!!

command +  c   不谢········


.h  文件

#import

NS_ASSUME_NONNULL_BEGIN

@interfaceGXW_BouncedWithdrawalView :UIView

+(void)BouncedRemindTitle:(NSString*)title;

@end

NS_ASSUME_NONNULL_END


.m 文件

#import "GXW_BouncedWithdrawalView.h"

#define window_width [UIScreen mainScreen].bounds.size.width

#define window_height [UIScreen mainScreen].bounds.size.height

#define app_window_width(value) ((value)/375.0f*[UIScreen mainScreen].bounds.size.width)//以6为基准

#define border_wide  8    //提示框 文字离黑框左右两边的距离

#define border_high  20  //提示框 文字离黑框上下两边的距离

#define text_font  15    //提示框 文字大小

#define max_width  290    //提示框 最大的宽度

#define txt_LineSpacing  6    //提示文字的行间距

#define txt_max_height  200  //提示文字的最大的高度

#define txt_One_height  50    //提示文字一行下的高度

#define txt_show_time  2      //提示文字显示时间

@interface GXW_BouncedWithdrawalView ()

@property (strong, nonatomic) UIButton *remindLable;

@end

@implementationGXW_BouncedWithdrawalView

static GXW_BouncedWithdrawalView *BouncedWithdrawalView;

+(void)BouncedRemindTitle:(NSString*)title{


    staticdispatch_once_tonceToken;

    dispatch_once(&onceToken, ^{

        BouncedWithdrawalView = [[GXW_BouncedWithdrawalView alloc] initWithFrame:CGRectMake(0, 0, window_width, window_height)];

    });


    //添加到界面上

    if (!BouncedWithdrawalView.superview) {

        [[UIApplication sharedApplication].keyWindow addSubview:BouncedWithdrawalView];

        [[UIApplication sharedApplication].keyWindow bringSubviewToFront:BouncedWithdrawalView];

    }


    BouncedWithdrawalView.hidden = NO;

    [BouncedWithdrawalView.remindLable setTitle:title forState:UIControlStateNormal];


    //计算文本文字size

    CGSize size = [title sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:text_font]}];

    CGFloatwidths = size.width+border_wide*2;

//    //获取宽高

//    CGSize statuseStrSize = CGSizeMake(ceilf(size.width), ceilf(size.height))

//    //打印宽高

    NSLog(@" %f  -----  %f",app_window_width(max_width),widths);

//    NSLog(@"文字:%@ \n %@  \nwidth:%f\nheight:%f",title,NSStringFromCGSize(statuseStrSize),size.width,size.height);

    if (widths >= app_window_width(max_width)) {

        [BouncedWithdrawalView addMultipleLinesTitle:app_window_width(max_width) TXT:title];

    }else{

        [BouncedWithdrawalViewaddAlineOf:widthsTXT:title];

    }

}

#pragma mark - 显示一行

-(void)addAlineOf:(CGFloat)widths TXT:(NSString*)TXT{

    //可变的属性文本

    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:TXT];


    //设置段落样式 使用NSMutableParagraphStyle类

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];


    paragraphStyle.alignment=NSTextAlignmentCenter;//文本对齐方式

    paragraphStyle.maximumLineHeight=txt_max_height;  //最大的行高

    paragraphStyle.lineSpacing=0;  //行自定义行高度


    //  给可变的属性字符串 添加段落格式

    [attributedTextaddAttribute:NSParagraphStyleAttributeNamevalue:paragraphStylerange:NSMakeRange(0, [TXTlength])];

    [attributedTextaddAttribute: NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, [TXT length])];

    [BouncedWithdrawalView.remindLable setAttributedTitle:attributedText forState:UIControlStateNormal];


    CGRectstart;

    start.size.height=txt_One_height;

    start.size.width= widths;

    BouncedWithdrawalView.remindLable.bounds = start;


    BouncedWithdrawalView.remindLable.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

//    BouncedWithdrawalView.remindLable.titleEdgeInsets = UIEdgeInsetsMake(border_high, border_wide, border_high-border_wide, border_wide);

    CGPoint point = CGPointMake(window_width/2, window_height/2);

    BouncedWithdrawalView.remindLable.center = point;

    [BouncedWithdrawalView addViewStart];


    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(txt_show_time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        BouncedWithdrawalView.hidden = YES;

    });

}

#pragma mark - 显示多行

-(void)addMultipleLinesTitle:(CGFloat)widths TXT:(NSString*)TXT{


    //可变的属性文本

    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:TXT];


    //设置段落样式 使用NSMutableParagraphStyle类

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];


    paragraphStyle.alignment=NSTextAlignmentLeft;//文本对齐方式

    paragraphStyle.maximumLineHeight=txt_max_height;  //最大的行高

    paragraphStyle.lineSpacing=txt_LineSpacing;  //行自定义行高度


    //  给可变的属性字符串 添加段落格式

    [attributedTextaddAttribute:NSParagraphStyleAttributeNamevalue:paragraphStylerange:NSMakeRange(0, [TXTlength])];

    [attributedTextaddAttribute: NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, [TXT length])];


    //将带有段落格式的可变的属性字符串给label.attributedText

    [BouncedWithdrawalView.remindLable setAttributedTitle:attributedText forState:UIControlStateNormal];


    CGSize size = [BouncedWithdrawalView.remindLable.titleLabel sizeThatFits:CGSizeMake(widths, txt_max_height)];

    CGRect start = BouncedWithdrawalView.remindLable.bounds;


//    NSLog(@"%@  \nwidth:%f\nheight:%f",NSStringFromCGSize(size),size.width,size.height);


    start.size.height= size.height+border_high*2;

    start.size.width= widths+border_wide*2;

    BouncedWithdrawalView.remindLable.bounds = start;


    CGPoint point = CGPointMake(window_width/2, window_height/2);

    BouncedWithdrawalView.remindLable.center = point;

    [BouncedWithdrawalView addViewStart];



    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(txt_show_time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        BouncedWithdrawalView.hidden = YES;

    });

}

-(instancetype)initWithFrame:(CGRect)frame{

    self= [superinitWithFrame:frame];

    self.backgroundColor = [UIColor clearColor];


    self.remindLable = [UIButton buttonWithType:UIButtonTypeCustom];

    self.remindLable.frame=CGRectMake(0,0,40,20);

    self.remindLable.center = self.center;

    [self addSubview:self.remindLable];



    self.remindLable.layer.cornerRadius = 4.0;

    self.remindLable.clipsToBounds = YES;

    self.remindLable.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.75];

    [self.remindLable setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    self.remindLable.titleLabel.font = [UIFont systemFontOfSize:text_font];

    self.remindLable.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

    self.remindLable.titleLabel.lineBreakMode = 0;

    self.remindLable.titleLabel.adjustsFontSizeToFitWidth = YES;


    return self;

}

-(void)addViewStart{

    CAKeyframeAnimation * animation;

    animation = [CAKeyframeAnimationanimationWithKeyPath:@"transform"];

    animation.duration=0.25;

    animation.removedOnCompletion = YES;

    animation.fillMode = kCAFillModeForwards;

    NSMutableArray *values = [NSMutableArray array];

    [valuesaddObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 1.0)]];

    [valuesaddObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];

    animation.values= values;

    [self.remindLable.layeraddAnimation:animationforKey:nil];

}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 29,828评论 8 265
  • 转自:http://www.code4app.com/blog-866962-1317.html1、设置UILab...
    MMOTE阅读 5,659评论 1 1
  • 宝贝明天要独自去北京了,贪睡的我一夜辗转难眠,他自己行不行,我要不要跟着?天刚蒙蒙亮,兴奋的小伙子就早早收拾利索催...
    627b2abd362a阅读 3,165评论 0 0
  • 哦啦啦
    莫名99阅读 2,695评论 0 0
  • 麦收时有人在两块麦田中间的水沟里发现了一对刺猬母子,以为好玩,老远就招呼我过去。 在农场干活的师傅对我说用铁锨掷到...
    初夏的时光阅读 3,638评论 18 12