热更新demo

updateContent.m 9.7KB

    // // updateContent.m // ThePaperDemo // // Created by scar1900 on 14/12/19. // Copyright (c) 2014年 scar1900. All rights reserved. // #import "updateContent.h" #define contentLabelTag 2000 #define updateBtnTag 1500 @interface updateContent () { CGFloat contentTotalHeight; NSString *orgIgnore; } @property(nonatomic, strong)UIView *backView; @property(nonatomic, strong)UIView *updateContentView; @property(nonatomic, strong)UIImageView *snapView; @property(nonatomic, strong)NSDictionary *updateInfo; @property(nonatomic, strong)UILabel *titleLabel; @property(nonatomic, strong)UIButton *selectButton; @end @implementation updateContent @synthesize snapView; @synthesize delegate; @synthesize updateInfo = _updateInfo; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; [self addSubview:self.backView]; self.clipsToBounds = YES; } return self; } - (void)presentShareContentInView:(UIView*)view updateInfo:(NSDictionary*)info{ [[UIApplication sharedApplication] beginIgnoringInteractionEvents]; self.updateInfo = info; self.snapView = nil; UIToolbar *toolBar = [[UIToolbar alloc]init]; if ([toolBar respondsToSelector:@selector(setBarTintColor:)]) { self.snapView = [[UIImageView alloc]initWithFrame:self.bounds]; self.backView.backgroundColor = [UIColor clearColor]; self.snapView.userInteractionEnabled = YES; self.snapView.image = customLightDarkBlurSnapshotFromView(view).image; self.snapView.alpha = 0; } toolBar = nil; [UIView animateWithDuration:0.25 animations:^{ if (self.snapView) { [self addSubview:snapView]; self.snapView.alpha = 1; }else { self.backView.alpha = 0.8; } [view addSubview:self]; } completion:^(BOOL finished) { [self addSubview:self.updateContentView]; [CoreAnimationEffect animationPushUp:self.updateContentView]; [[UIApplication sharedApplication] endIgnoringInteractionEvents]; }]; } - (UIView *)backView { if (!_backView) { _backView = [[UIView alloc]initWithFrame:self.bounds]; _backView.alpha = 0; _backView.backgroundColor = [UIColor colorWithHexString:@"0x3e3c3d"]; } return _backView; } - (UIView *)updateContentView { if (!_updateContentView) { _updateContentView = [[UIView alloc]initWithFrame:CGRectZero]; _updateContentView.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; } return _updateContentView; } - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.updateContentView.bounds), 50)]; _titleLabel.textAlignment = NSTextAlignmentCenter; _titleLabel.textColor = [UIColor colorWithHexString:TextBlack]; _titleLabel.font = appFont(35/2, NO); } return _titleLabel; } - (UIButton*)selectButton { if (!_selectButton) { _selectButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_selectButton setImage:Image(@"login/selectedIcon.png") forState:UIControlStateSelected]; [_selectButton setImage:Image(@"login/unselectedIcon.png") forState:UIControlStateNormal]; _selectButton.backgroundColor = [UIColor clearColor]; [_selectButton addTarget:self action:@selector(ignoreVersion:) forControlEvents:UIControlEventTouchUpInside]; [_selectButton setImageEdgeInsets:UIEdgeInsetsMake(15/4, 15/4, 15/4, 15/4+90)]; } return _selectButton; } - (void)setUpdateInfo:(NSDictionary *)info { _updateInfo = info; NSArray *contentList = info[@"updateDesc"]; __block CGFloat totalHeight=0; [contentList enumerateObjectsUsingBlock:^(NSString* desc, NSUInteger idx, BOOL *stop) { CGFloat height = heightForString(desc, appFont(13, NO), 250, NSLineBreakByWordWrapping)+15/2; totalHeight = totalHeight+height; }]; contentTotalHeight = totalHeight; CGFloat height = 158/2 + contentTotalHeight + 175/2; CGFloat width = 300; self.updateContentView.frame = CGRectMake(self.center.x-width/2, CGRectGetHeight(self.bounds)-height-20, width, height); NSString *titleStr = [NSString stringWithFormat:@"%@%@",info[@"updateTitle"],info[@"versionName"]]; self.titleLabel.text = titleStr; [self.updateContentView addSubview:self.titleLabel]; UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame), CGRectGetWidth(self.updateContentView.bounds) , 0.5)]; line.backgroundColor = [UIColor colorWithHexString:LINECOLOR]; [self.updateContentView addSubview:line]; [self addConentLabel:contentList]; [self.updateContentView addSubview:self.selectButton]; self.selectButton.frame = CGRectMake(15, CGRectGetHeight(self.updateContentView.bounds)-125/2 - 30, 46/2+90, 46/2); /** * bug:5057(启动页升级提示,“下次不再提醒”打钩的热区变大) */ UILabel *ignoreLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.selectButton.frame)-90, CGRectGetMinY(self.selectButton.frame), 90, 23)]; ignoreLabel.text = @"此版本不再提示"; ignoreLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; ignoreLabel.font = appFont(TEXT_SIX_LEVELSIZE, NO); [self.updateContentView addSubview:ignoreLabel]; TPCustomButton *updateButton = [[TPCustomButton alloc]initWithFrame:CGRectMake(20, CGRectGetHeight(self.updateContentView.bounds)-125/2, 365/2, 40)]; updateButton.title = @"立即更新"; updateButton.tag = updateBtnTag; [updateButton addTarget:self action:@selector(toUpdate:) forControlEvents:UIControlEventTouchUpInside]; [self.updateContentView addSubview:updateButton]; TPCustomButton *canselButton = [[TPCustomButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(updateButton.frame)+15/2, CGRectGetHeight(self.updateContentView.bounds)-125/2, 70, 40)]; canselButton.title = @"取消"; [canselButton addTarget:self action:@selector(canselUpdate:) forControlEvents:UIControlEventTouchUpInside]; [self.updateContentView addSubview:canselButton]; } - (void)addConentLabel:(NSArray*)contentArray { [contentArray enumerateObjectsUsingBlock:^(NSString* desc, NSUInteger idx, BOOL *stop) { UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero]; label.text = desc; label.textAlignment = NSTextAlignmentLeft; label.textColor = [UIColor colorWithHexString:TextGray]; label.font = appFont(13, NO); label.lineBreakMode = NSLineBreakByWordWrapping; label.numberOfLines = 0; label.tag = contentLabelTag + idx; CGFloat height = heightForString(desc, appFont(13, NO), 250, NSLineBreakByWordWrapping); UILabel *lastLabel = (UILabel*)[self.updateContentView viewWithTag:contentLabelTag+idx-1]; if (!lastLabel) { label.frame = CGRectMake(30, 158/2, 250, height); }else { label.frame = CGRectMake(30, CGRectGetMaxY(lastLabel.frame)+15/2, 250, height); } [self.updateContentView addSubview:label]; UIView *round = [[UIView alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(label.frame)-height/2, 5/2, 5/2)]; round.backgroundColor = [UIColor colorWithHexString:TextGray]; [self.updateContentView addSubview:round]; }]; } - (void)dismissUpdateContent { [UIView animateWithDuration:0.25 animations:^{ self.updateContentView.frame = CGRectMake(self.center.x-150, CGRectGetHeight(self.bounds), 300, CGRectGetHeight(self.updateContentView.bounds)); self.snapView.alpha = 0; self.backView.alpha = 0; } completion:^(BOOL finished) { [self.updateContentView removeFromSuperview]; [self.backView removeFromSuperview]; [self.snapView removeFromSuperview]; [self removeFromSuperview]; if ([self.delegate respondsToSelector:@selector(didDismissUpdateContent:)]) { [self.delegate didDismissUpdateContent:self]; } }]; } - (void)ignoreVersion:(UIButton*)btn { btn.selected = !btn.selected; if (btn.selected) { orgIgnore = [TPUserDefault instance].ignoreVersion; [TPUserDefault instance].ignoreVersion = self.updateInfo[@"versionName"]; TPCustomButton *updateBtn = (TPCustomButton*)[self.updateContentView viewWithTag:updateBtnTag]; updateBtn.enabled = NO; }else { [TPUserDefault instance].ignoreVersion = orgIgnore; TPCustomButton *updateBtn = (TPCustomButton*)[self.updateContentView viewWithTag:updateBtnTag]; updateBtn.enabled = YES; } } - (void)toUpdate:(UIButton*)btn { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.updateInfo[@"address"]]]; } - (void)canselUpdate:(UIButton*)btn { [self dismissUpdateContent]; } @end