// ViewController.m
// 应用管理
//
// Created by 李博 on 16/1/15.
// Copyright © 2016年 libo. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
//创建一个数组
@property(nonatomic,strong)NSArray *apps;
@end
@implementation ViewController
-(NSArray *)apps{
//重写get方法懒加载
if (_apps==nil) {
NSString *str =[[NSBundle mainBundle]pathForResource:@"apps.plist" ofType:nil];
_apps=[NSArray arrayWithContentsOfFile:str];
}
return _apps;
}
- (void)viewDidLoad {
[super viewDidLoad];
//计算九宫格
int geShu=3;
CGFloat cgh=90;
CGFloat cgw=75;
CGFloat ychu=30;
CGFloat x=(320-geShu*cgw)/(geShu+1);
for (int i=0; i<self.apps.count; i++) {
//获取每个应用的数据
//创建字典
NSDictionary *dict=self.apps[i];
//创建一个View
UIView *view1 =[[UIView alloc]init];
// view1.backgroundColor =[UIColor redColor];
CGFloat cgx=x + (i%geShu)*(x+cgw);
CGFloat cgy=ychu + (i/geShu)*(ychu+cgh);
view1.frame=CGRectMake(cgx, cgy, cgw, cgh);
//传进界面中
[self.view addSubview:view1];
//创建一个imageview
UIImageView *imgviewIcon =[[UIImageView alloc]init];
imgviewIcon.image=[UIImage imageNamed:dict[@"icon"]];
CGFloat iconW=45;
CGFloat iconH=iconW;
CGFloat iconY=0;
CGFloat iconX=(cgw-iconW)*0.5;
imgviewIcon.frame=CGRectMake(iconX, iconY, iconW, iconH);
[view1 addSubview:imgviewIcon];
//创建一个lable
UILabel *lblName =[[UILabel alloc]init];
lblName.text =dict[@"name"];
lblName.font=[UIFont systemFontOfSize:12];
lblName.textAlignment=NSTextAlignmentCenter;
CGFloat nameW=cgw;
CGFloat nameH=21;
CGFloat nameX=0;
CGFloat nameY=iconH;
lblName.frame=CGRectMake(nameX, nameY, nameW, nameH);
[view1 addSubview:lblName];
//创建一个button
UIButton *button=[[UIButton alloc]init];
[button setTitle:@"下载" forState:UIControlStateNormal];
[button setTitle:@"已下载" forState:UIControlStateDisabled];
[button setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
[button setBackgroundImage: [UIImage imageNamed:@"buttongreen_highlighted" ]forState:UIControlStateDisabled];
[view1 addSubview:button];
button.titleLabel.font=[UIFont systemFontOfSize:14];
CGFloat btnW=45;
CGFloat btnH=25;
CGFloat btX=(cgw-btnW)*0.5;
CGFloat btY=iconH+nameH;
button.frame=CGRectMake(btX, btY, btnW, btnH);
//为按钮注册一个单击事件
[button addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
[view1 addSubview:button];
}
}
-(void)didClickButton:(UIButton *)sender{
//设置当前控制器的view不允许和用户交互
self.view.userInteractionEnabled=NO;
sender.enabled=NO;
//需要一个lable
UILabel *lbl=[[UILabel alloc]init];
//设置按钮属性
lbl.frame=CGRectMake(70, 250, 180, 35);
lbl.backgroundColor=[UIColor blackColor];
lbl.text=@"正在下载...";
lbl.textColor=[UIColor whiteColor];
lbl.font=[UIFont systemFontOfSize:16];
lbl.textAlignment=NSTextAlignmentCenter;
lbl.alpha=0.7;
lbl.layer.cornerRadius=5;
lbl.layer.masksToBounds=YES;
[self.view addSubview:lbl];
[UIView animateWithDuration:1.5 animations:^{
lbl.alpha=0.0;
} completion:^(BOOL finished){
[lbl removeFromSuperview];
self.view.userInteractionEnabled=YES;
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end