一个简单的类,用于检测app版本更新的弹出提示框

作者: 熊猫小贼_ | 来源:发表于2016-08-02 17:04 被阅读1057次

    废话不多说了,这个类突发奇想,自己写写的,可用于很多地方的弹窗,我这里做更新的提示框用,需要其他地方用的话在类里面写个delegate应该就可以了!我写的比较简单!直接上代码!

    UpdateAlterView.h

    //
    //  UpdateAlterView.h
    //  MyProject
    //
    //  Created by danggui on 16/7/19.
    //  Copyright © 2016年 danggui. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface UpdateAlterView : UIView
    
    - (void)show:(NSString  *)str;
    
    @end
    

    UpdateAlterView.m

    //
    //  UpdateAlterView.m
    //  MyProject
    //
    //  Created by danggui on 16/7/19.
    //  Copyright © 2016年 danggui. All rights reserved.
    //
    
    #import "UpdateAlterView.h"
    
    #define W CGRectGetWidth([UIScreen mainScreen].bounds)
    #define H CGRectGetHeight([UIScreen mainScreen].bounds)
    
    @interface UpdateAlterView ()
    
    @property (nonatomic, strong) UIView *bigView;
    @property (nonatomic, strong) UIImageView *lab;
    @property (nonatomic, strong) UILabel *text;
    @property (nonatomic, strong) NSMutableString *textStr;
    
    @end
    
    @implementation UpdateAlterView
    @synthesize lab,text;
    
    - (instancetype)init
    {
        self = [super init];
        if (self) {
            self.frame = [UIScreen mainScreen].bounds;
            self.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
            
            self.bigView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, W - 40, 150)];
            _bigView.layer.cornerRadius = 10;
            _bigView.layer.masksToBounds = YES;
            _bigView.backgroundColor = [UIColor whiteColor];
            _bigView.center = CGPointMake(W / 2, H / 2);
            
            text = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_bigView.frame) - 30, 25)];
            text.center = CGPointMake(CGRectGetWidth(_bigView.frame)/2, CGRectGetHeight(_bigView.frame)/2);
            text.text = @"";
            text.textAlignment = NSTextAlignmentCenter;
            [_bigView addSubview:text];
            
            lab = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
            lab.backgroundColor = [UIColor whiteColor];
            lab.layer.cornerRadius = 40;
            lab.layer.masksToBounds = YES;
            [lab setImage:[UIImage imageNamed:@"image5.jpg"]];
            lab.center =  CGPointMake(self.center.x , self.center.y - CGRectGetHeight(_bigView.frame)/2 + 20);
            
            [self addSubview:_bigView];
            [self addSubview:lab];
            
            UIButton *bu1 = [UIButton buttonWithType:UIButtonTypeCustom];
            bu1.layer.borderColor = [UIColor lightGrayColor].CGColor;
            bu1.layer.borderWidth = .3f;
            bu1.bounds = CGRectMake(0, 0, CGRectGetWidth(_bigView.frame)/ 2, 44);
            bu1.center = CGPointMake(CGRectGetWidth(_bigView.bounds)/4, CGRectGetHeight(_bigView.bounds) - 44 + 24);
            [bu1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [bu1 addTarget:self action:@selector(button1) forControlEvents:UIControlEventTouchUpInside];
            
            [bu1 setTitle:@"取消" forState:UIControlStateNormal];
            [_bigView addSubview:bu1];
            
            UIButton *bu2 = [UIButton buttonWithType:UIButtonTypeCustom];
            bu2.layer.borderColor = [UIColor lightGrayColor].CGColor;
            bu2.layer.borderWidth = .2f;
            bu2.bounds = CGRectMake(0, 0, CGRectGetWidth(_bigView.frame)/ 2, 44);
            bu2.center = CGPointMake(3 * CGRectGetWidth(_bigView.bounds)/4, CGRectGetHeight(_bigView.bounds) - 44 + 24);
            [bu2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [bu2 setTitle:@"确定" forState:UIControlStateNormal];
            [bu2 addTarget:self action:@selector(button2) forControlEvents:UIControlEventTouchUpInside];
            [_bigView addSubview:bu2];
            UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(0, bu2.frame.origin.y, CGRectGetWidth(_bigView.bounds), 0.5)];
            line.backgroundColor = [UIColor colorWithRed:0.8235 green:0.8235 blue:0.8235 alpha:1.0];
            [_bigView addSubview:line];
            
            _bigView.transform = CGAffineTransformMakeScale(0, 0);
            lab.transform = CGAffineTransformMakeScale(0,0);
        
        }
        return self;
    }
    
    - (void)button2 {
        [self dissmiss];
        //ReUploadUrl  为你跳转到AppStore的链接
        //#define ReUploadUrl @"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id="
        //(ps:id=后面的就是你提交appstore审核的那个id,找不到的自行百度)
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:ReUploadUrl]];
    }
    
    - (void)button1 {
        [self dissmiss];
    }
    
    - (void)dissmiss {
        [UIView animateWithDuration:.2 animations:^{
            [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
            self.alpha = 0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }
    
    - (void)show:(NSString  *)str{
        UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
        [window addSubview:self];
        [UIView animateWithDuration:.3 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 / 0.8 options:0 animations:^{
            [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
            //        self.alpha = 1;
            _bigView.transform = CGAffineTransformIdentity;
            lab.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            [text setText:str];
        }];
    }
    
    @end
    

    使用也很简单

    在你需要用到的地方
    #import "UpdateAlterView.h"
    
    然后
    UpdateAlterView *update = [[UpdateAlterView alloc]init];
    [update show:@"有新版本更新哦!😄"];
    
    就好了
    如图:
    
    Paste_Image.png

    相关文章

      网友评论

      • 马铃薯蜀黍:你好,一个小建议,不知道该不该说,如果不对,当我没说,建议你文字先简单介绍一下逻辑,因为大段的UI大部分人不必要阅读的,如果一下能了解作者的思路,我想你的文章一定很畅销,感谢分享
        熊猫小贼_:@马铃薯蜀黍 谢谢您的建议 以后我会对一些功能点做说明:blush:

      本文标题:一个简单的类,用于检测app版本更新的弹出提示框

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