#import "ViewController.h"
#import "CZApp.h"
#import "CZAppView.h"
@interface ViewController ()
@property(nonatomic,strong)NSArray *appArray;
@end
@implementation ViewController
//懒加载
-(NSArray *)appArray
{
if(_appArray == nil)
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"app" ofType:@"plist"];
NSArray *arr = [[NSArray alloc] init];
arr = [NSArray arrayWithContentsOfFile:path];
//字典转模型
//1.定义可变数组
NSMutableArray *Marr = [NSMutableArray array];
//2.遍历字典数组
for(NSDictionary *dict in arr)
{
//字典转模型
CZApp *app = [CZApp appWithDict:dict];
//将模型添加到可变数组中
[Marr addObject:app];
}
//将模型数组赋值给字典数字
_appArray = Marr;
}
return _appArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
#define KUIViewCol 3
#define KUIViewW 80
#define KUIViewH 100
CGFloat Margin = (self.view.frame.size.width - KUIViewCol*KUIViewW)/(KUIViewCol+1);
for(int i = 0;i<self.appArray.count;i++)
{
NSInteger row = i/KUIViewCol;
NSInteger col = i%KUIViewCol;
CGFloat viewX = Margin + col*(KUIViewW+Margin);
CGFloat viewY = Margin + row*(KUIViewH+Margin);
//1.创建控件对象
//UIView *appView = [UIView alloc]init];等同于下面
CZAppView *appView =[[[NSBundle mainBundle]loadNibNamed:@"CZAppView" owner:nil options:nil] lastObject];
//2.设置属性,优先设置frame
appView.frame = CGRectMake(viewX, viewY, appView.bounds.size.width, appView.bounds.size.height);
//2.1 获取数据
CZApp *app = self.appArray[i];
//2.2传入数据
[appView setApp:app];
//3.添加到对应控件中
[self.view addSubview:appView];
}
}
@end
//CZAPPView类
#import "CZAppView.h"
#import "CZApp.h"
@interface CZAppView ()
@property (weak, nonatomic) IBOutlet UIImageView *iconImage;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
- (IBAction)btnClick:(UIButton *)sender;
@end
@implementation CZAppView
- (void)setApp:(CZApp *)app
{
self.iconImage.image = [UIImage imageNamed:app.icon];
self.nameLabel.text = app.name;
}
- (IBAction)btnClick:(UIButton *)sender
{
//1.让按钮禁用
sender.enabled = NO;
//2.让屏幕中间显示一个文字
UILabel *label = [[UILabel alloc]init];
//设置属性
label.bounds = CGRectMake(0, 0, 100, 30);
label.center = self.superview.center;
//设置背景颜色
label.backgroundColor = [UIColor blackColor];
//设置文字
label.text = @"正在下载...";
label.textColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:13];
label.textAlignment = NSTextAlignmentCenter;
label.alpha = 0;
//添加到对应控件中
[self.superview addSubview:label];
//添加动画效果
[UIView animateWithDuration:2 animations:^{
label.alpha = 1;
} completion:^(BOOL finished) {
if(finished)
{
// [UIView animateWithDuration:2 animations:^{
// label.alpha = 0;
// } completion:^(BOOL finished) {
// if(finished)
// {
// [label removeFromSuperview];
// }
// }];
[UIView animateWithDuration:2.0 delay:2.0 options:UIViewAnimationOptionCurveLinear animations:^{
label.alpha = 0;
} completion:^(BOOL finished) {
[label removeFromSuperview];
}];
}
}];
}
@end