|
//
// updateContent.m
// ThePaperDemo
//
// Created by scar1900 on 14/12/19.
// Copyright (c) 2014年 scar1900. All rights reserved.
//
#import "updateContent.h"
#import "KGModal.h"
#define contentLabelTag 2000
#define updateBtnTag 1500
@interface updateContent () {
CGFloat contentTotalHeight;
NSString *orgIgnore;
}
@property(nonatomic, strong)UIView *updateContentView;
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UIButton *selectButton;
@end
@implementation updateContent
@synthesize updateInfo = _updateInfo;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.clipsToBounds = YES;
[self addSubview:self.updateContentView];
self.updateContentView.frame = frame;
[KGModal sharedInstance].tapOutsideToDismiss = NO;
[KGModal sharedInstance].closeButtonType = KGModalCloseButtonTypeNone;
[TPUserDefault instance].updateInfo = nil;
}
return self;
}
- (UIView *)updateContentView {
if (!_updateContentView) {
_updateContentView = [[UIView alloc]initWithFrame:CGRectZero];
_updateContentView.backgroundColor = [UIColor whiteColor];
}
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)];
}
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), 540/2, NSLineBreakByWordWrapping)+15/2;
totalHeight = totalHeight+height;
}];
contentTotalHeight = totalHeight;
CGFloat height = 158/2 + contentTotalHeight + 175/2;
self.updateContentView.frame = CGRectMake(10, CGRectGetHeight(self.bounds)-height-20, CGRectGetWidth(self.bounds)-20, 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) ,
1)];
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 - 35, 46/2, 46/2);
UILabel *ignoreLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.selectButton.frame), CGRectGetMinY(self.selectButton.frame), 90, 23)];
ignoreLabel.text = @"此版本不再提示";
ignoreLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY];
ignoreLabel.font = appFont(10, NO);
[self.updateContentView addSubview:ignoreLabel];
TPCustomButton *updateButton = [[TPCustomButton alloc]initWithFrame:CGRectMake(20,
CGRectGetHeight(self.updateContentView.bounds)-125/2,
365/2,
40)];
updateButton.title = @"立即更新";
[updateButton addTarget:self action:@selector(toUpdate:) forControlEvents:UIControlEventTouchUpInside];
updateButton.tag = updateBtnTag;
[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];
self.updateContentView.frame = self.bounds;
self.updateContentView.layer.cornerRadius = 3;
line.frame =CGRectMake(0,
CGRectGetMaxY(self.titleLabel.frame),
CGRectGetWidth(self.updateContentView.bounds) ,
0.5);
}
- (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), 540/2, NSLineBreakByWordWrapping);
UILabel *lastLabel = (UILabel*)[self.updateContentView viewWithTag:contentLabelTag+idx-1];
if (!lastLabel) {
label.frame = CGRectMake(30, 70, 540/2, height);
}else {
label.frame = CGRectMake(30, CGRectGetMaxY(lastLabel.frame)+15/2, 540/2, 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)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 {
[[KGModal sharedInstance]hideAnimated:YES];
}
@end
|