美文网首页
2018-02-26 快速写一个信息提示View

2018-02-26 快速写一个信息提示View

作者: 破夕_____________ | 来源:发表于2018-02-26 14:55 被阅读17次
    未点击.png 点击之后的.png
    @property (nonatomic, weak) NSTimer *hideDelayTimer;
    
    @property (nonatomic,strong) UIView *customView;
    @property (nonatomic,strong) UILabel *warnLabel;
    @property (nonatomic,strong) NSString *imageStr;
    @property (nonatomic,strong) NSString *textStr;
    @property (nonatomic,strong) UIImageView *imageView;
    
    +(void)showSuccess:(NSString *)success;
    
    +(void)showError:(NSString *)error;
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        if (self = [super initWithFrame:frame]) {
    //        灰色背景
            self.customView = [[UIView alloc] initWithFrame:CGRectMake((frame.size.width - 350 *KScale)/2, (frame.size.height - 188 *KScale)/2, 350 *KScale, 188 *KScale)];
            self.customView.backgroundColor =[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.7];
            self.customView.layer.cornerRadius = 15 *KScale;
            self.customView.layer.masksToBounds = YES;
            [self addSubview:self.customView];
    //        灰色image
            self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(155 *KScale, 30 *KScale, 44 *KScale, 44 *KScale)];
            self.imageView.backgroundColor = [UIColor clearColor];
            [self.customView addSubview:self.imageView];
    //        提示文字
            self.warnLabel = [[UILabel alloc] initWithFrame:CGRectMake(25 *KScale, 78 *KScale, 300 *KScale, 80 *KScale)];
            self.warnLabel.textColor = [UIColor whiteColor];
            self.warnLabel.font = [UIFont systemFontOfSize:28 *KScale];
            self.warnLabel.textAlignment = NSTextAlignmentCenter;
            self.warnLabel.backgroundColor = [UIColor clearColor];
            self.warnLabel.numberOfLines = 0;
            [self.customView addSubview:self.warnLabel];
            
        }
        return self;
    }
    
    + (void)showSuccess:(NSString *)success
    {
        [self showText:success imageStr:@""];
    }
    
    +(void)showError:(NSString *)error
    {
        [self showText:error imageStr:@""];
    }
    
    + (void)showText:(NSString *)text imageStr:(NSString *)imageStr
    {
        SXWShowMessageView *showMessageView = [SXWShowMessageView showViewAndAnimated:YES];
        showMessageView.imageStr = imageStr;
        showMessageView.textStr = text;
    //    动画消失 1.2秒之后再消失
        [showMessageView hideAnimated:YES afterDelay:1.2];
    }
    #pragma mark 加载
    + (instancetype)showViewAndAnimated:(BOOL)nimated
    {
        UIWindow *keyWindow = [[[UIApplication sharedApplication]delegate] window];
        keyWindow.windowLevel = UIWindowLevelNormal;
        
        SXWShowMessageView *showMessageView = [[self alloc] initWithFrame:keyWindow.bounds];
        showMessageView.center = CGPointMake(keyWindow.bounds.size.width/2.0f, keyWindow.bounds.size.height/2.0f);
        [keyWindow addSubview:showMessageView];
        [showMessageView showAnimated:YES];
        return showMessageView;
    }
    //动画展示
    - (void)showAnimated:(BOOL)animated
    {
        self.alpha = 0;
        [UIView animateWithDuration:.35 animations:^{
            self.alpha = 1;
        }];
    }
    #pragma mark 消失
    - (void)hideAnimated:(BOOL)animated afterDelay:(NSTimeInterval)afterDelay
    {
        NSTimer *timer = [NSTimer timerWithTimeInterval:afterDelay target:self selector:@selector(handleTimer:) userInfo:@(animated) repeats:NO];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
        self.hideDelayTimer = timer;
        
    }
    - (void)handleTimer:(NSTimer *)timer
    {
        [self dismissAnimation];
    }
    //动画消失
    - (void)dismissAnimation
    {
        self.alpha = 1;
        [UIView animateWithDuration:.35 animations:^{
            self.alpha = 0;
        }completion:^(BOOL finished) {
            [self.hideDelayTimer invalidate];
            self.hideDelayTimer = nil;
            [self removeFromSuperview];
        }];
    }
    
    //赋值
    - (void)setImageStr:(NSString *)imageStr
    {
        self.imageView.image = [UIImage imageNamed:imageStr];
    }
    - (void)setTextStr:(NSString *)textStr
    {
        self.warnLabel.text = textStr;
        
        CGRect rect = [textStr boundingRectWithSize:CGSizeMake(300*KScale, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_warnLabel.font} context:nil];
        CGFloat height = rect.size.height;
        CGFloat h = height+78*KScale+30*KScale;
        
        self.customView.frame = CGRectMake((self.frame.size.width-350*KScale)/2, (self.frame.size.height-h)/2, 350*KScale, h);
        self.warnLabel.frame = CGRectMake(25*KScale, 78*KScale, 300*KScale, height);
        
    }
    
    

    相关文章

      网友评论

          本文标题:2018-02-26 快速写一个信息提示View

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