1 //
2 // LWTViewController.m
3 // 应用管理--九宫格
4 //
5 // Created by apple on 14-5-22.
6 // Copyright (c) 2014年 lwt. All rights reserved.
7 //
8
9 #import "LWTViewController.h"
10
11 @interface LWTViewController ()
12
13 // 保存plist文件内容
14 @property (nonatomic, strong) NSArray *appInfoList;
15
16 @end
17
18 @implementation LWTViewController
19
20 - (NSArray *)appInfoList
21 {
22 if (!_appInfoList) {
23 // 根据Plist文件获取内容
24 NSBundle *bundle = [NSBundle mainBundle];
25 NSString *path = [bundle pathForResource:@"app.plist" ofType:nil];
26
27 _appInfoList = [NSArray arrayWithContentsOfFile:path];
28 }
29 return _appInfoList;
30
31 }
32
33 - (void)viewDidLoad
34 {
35 [super viewDidLoad];
36 // Do any additional setup after loading the view, typically from a nib.
37
38 // 每行3列
39 int totalcols = 3;
40 // view宽高
41 CGFloat viewWidth = 80;
42 CGFloat viewHeight = 90;
43 // 两个view的宽度间隔
44 CGFloat viewM = (self.view.bounds.size.width - (viewWidth * totalcols)) / (totalcols + 1);
45 // 起始高度
46 CGFloat viewStart = 20;
47 // 高度间隔
48 CGFloat viewH = 10;
49
50 for (int i = 0; i < self.appInfoList.count; i++) {
51 // 行数
52 int row = i / totalcols;
53 // 列数
54 int col = i % totalcols;
55 // 每个view的x
56 int viewX = viewM + ((viewWidth + viewM)* col);
57 // 每个view的y
58 int viewY = viewStart + viewH + (viewH + viewHeight) * row;
59
60 // 创建一个UIView
61 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(viewX, viewY, viewWidth, viewHeight)];
62 [self.view addSubview:view];
63
64 // 该条目的title和图片名称
65 NSDictionary *dict = self.appInfoList[i];
66
67 // 向view里添加图片
68 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, viewWidth, 50)];
69 // 添加图片
70 imageView.image = [UIImage imageNamed:dict[@"icon"]];
71 // 图片自适应
72 imageView.contentMode = UIViewContentModeScaleAspectFit;
73
74 [view addSubview:imageView];
75
76 // 向view里添加label
77 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, imageView.bounds.size.height, viewWidth, 20)];
78 // title
79 label.text = dict[@"name"];
80 // 字体大小
81 label.font = [UIFont systemFontOfSize:12.0];
82 // 居中
83 label.textAlignment = NSTextAlignmentCenter;
84
85 [view addSubview:label];
86
87 // 向view里添加button
88 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
89 button.frame = CGRectMake(15, view.bounds.size.height - 20, 50, 20);
90
91 // 默认背景
92 [button setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
93 // 高亮背景
94 [button setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];
95 // title
96 [button setTitle:@"下载" forState:UIControlStateNormal];
97 // 字体大小
98 button.titleLabel.font = [UIFont systemFontOfSize:13.0];
99 // 监听点击事件
100 [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
101 // 设置tag
102 button.tag = i;
103
104 [view addSubview:button];
105 }
106 }
107
108 - (void) buttonClick: (UIButton *)button
109 {
110 // 创建弹窗,提示下载完成
111 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(80, 360, self.view.bounds.size.width - 80 *2, 40)];
112 // 弹窗背景颜色
113 label.backgroundColor = [UIColor lightGrayColor];
114 // 设置文字
115 label.font = [UIFont systemFontOfSize:14.0];
116 label.text = [NSString stringWithFormat:@"安装 %@ 成功",self.appInfoList[button.tag][@"name"]];
117 label.textAlignment = NSTextAlignmentCenter;
118 label.textColor = [UIColor whiteColor];
119
120 label.alpha = 0.0;
121 // 向UIView中添加弹窗
122 [self.view addSubview:label];
123
124 [UIView animateWithDuration:1.2 animations:^{
125 label.alpha = 1.0;
126 button.enabled = 0;
127 [button setTitle:@"已下载" forState:UIControlStateDisabled];
128 } completion:^(BOOL finished) {
129 [UIView animateWithDuration:1.0 animations:^{
130 label.alpha = 0.0;
131 }];
132 }];
133 }
134
135 @end