弹出发现新版本View具体实现

效果图:


屏幕快照 2019-03-25 下午3.43.26.png

具体调用部分

if (currVersionInt<serviceVersionInt) {
            SCVersionUpdateView *tipView = [[SCVersionUpdateView alloc] initWithVersion:result[@"required_version"] updateType:result[@"type"] updateCon:result[@"update_content"] updateUrl:result[@"update_url"]];
            [tipView show];
            block?block(0):nil;
        }else{
            block?block(1):nil;
        }

具体实现:
.h

@interface SCVersionUpdateView : UIView
- (instancetype)initWithVersion:(NSString *)version updateType:(NSString *)updateType updateCon:(NSString *)updateCon updateUrl:(NSString *)updateUrl;
- (void)show;

@end

.m

@interface SCVersionUpdateView()
@property (nonatomic,copy) NSString *updateUrl;
@end


@implementation SCVersionUpdateView

- (instancetype)initWithVersion:(NSString *)version updateType:(NSString *)updateType updateCon:(NSString *)updateCon updateUrl:(NSString *)updateUrl{
    
    if (self = [super init]) {
        self.updateUrl = updateUrl;
        
        self.frame = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds));
        UIView *backView = [[UIView alloc] initWithFrame:self.frame];
        backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        [self addSubview:backView];
        
        UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(48, ( CGRectGetHeight([UIScreen mainScreen].bounds) - 340) / 2, CGRectGetWidth([UIScreen mainScreen].bounds) - 48*2, 340)];
        mainView.backgroundColor = [UIColor whiteColor];
        mainView.layer.masksToBounds = YES;
        mainView.layer.cornerRadius = 7;
        
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"sc_version_update"]];
        [imageView setFrame:CGRectMake(0, 0, CGRectGetWidth(mainView.frame),CGRectGetWidth(mainView.frame)* 149/305)];
        [mainView addSubview:imageView];


        UILabel *updateConLab = [[UILabel alloc] init];
        updateConLab.text = [NSString stringWithFormat:@"版本%@",version];
        updateConLab.font = [UIFont boldSystemFontOfSize:16];
        updateConLab.frame = CGRectMake(15, imageView.frame.size.height+imageView.frame.origin.y+7 + 10,200, 22);
        updateConLab.textColor = [self colorWithHexString:@"#333333"];
        [mainView addSubview:updateConLab];

        UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(20, updateConLab.frame.size.height+updateConLab.frame.origin.y+16, mainView.frame.size.width-40, 150)];
        textView.textColor = [self colorWithHexString:@"#888888"];
        textView.font = [UIFont systemFontOfSize:14];
        textView.text = updateCon;
        textView.editable = NO;
        [mainView addSubview:textView];
        
        UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake((mainView.frame.size.width-140)/2, mainView.frame.size.height - 60, 140, 40)];
        confirmBtn.layer.cornerRadius = 20;
        [confirmBtn setBackgroundColor:[self colorWithHexString:@"#347AEB"]];
        [confirmBtn setTitle:@"立即更新" forState:UIControlStateNormal];
        [confirmBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [mainView addSubview:confirmBtn];
        [confirmBtn addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
        
//        if (![updateType isEqualToString:@"force"]) {
            UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(mainView.bounds.size.width/2-118, mainView.bounds.size.height-40-20, 113, 40)];
            [cancelBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
            [cancelBtn setTitleColor:[self colorWithHexString:@"#666666"] forState:UIControlStateNormal];
            [cancelBtn setTitle:@"暂不更新" forState:UIControlStateNormal];
            cancelBtn.layer.cornerRadius = 5;
            cancelBtn.layer.borderColor = [[self colorWithHexString:@"#ECECEC"] CGColor];
            cancelBtn.layer.borderWidth = 1;
            [mainView addSubview:cancelBtn];
            [cancelBtn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
            
            [confirmBtn setFrame:CGRectMake(mainView.bounds.size.width/2+5, mainView.bounds.size.height-40-20, 113, 40)];
            confirmBtn.layer.cornerRadius = 5;
//        }
        
        [self addSubview:mainView];
        
    }
    return self;
}

- (UIColor *) colorWithHexString: (NSString *)color{
    
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    // 判断前缀
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    // 从六位数值中找到RGB对应的位数并转换
    NSRange range;
    range.location = 0;
    range.length = 2;
    //R、G、B
    NSString *rString = [cString substringWithRange:range];
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}


- (void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

//去苹果商店更新
- (void)confirm{
    NSURL *url = [NSURL URLWithString:self.updateUrl];
    [[UIApplication sharedApplication] openURL:url];
}

- (void)cancel{
    [self removeFromSuperview];
}
@end

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

推荐阅读更多精彩内容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,724评论 0 3
  • 1 CALayer IOS SDK详解之CALayer(一) http://doc.okbase.net/Hell...
    Kevin_Junbaozi阅读 5,203评论 3 23
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,140评论 1 32
  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,768评论 0 9
  • 文/胖哥你好 炎热的夏日,光照有些耀眼。大街上,行人匆匆,花红柳绿的遮阳伞,却阻挡不了汗水的溢出。无奈下,人们便减...
    胖哥你好阅读 472评论 3 8