美文网首页
iOS 弹框提示 2秒隐藏

iOS 弹框提示 2秒隐藏

作者: 已丶惘然 | 来源:发表于2018-11-29 16:56 被阅读58次

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

    写的不好,请多多指点!

    调用:[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

    相关文章

      网友评论

          本文标题:iOS 弹框提示 2秒隐藏

          本文链接:https://www.haomeiwen.com/subject/tznmgftx.html