// // YJAppInfo.h // 05-应用程序管理的练习 // // Created by JACKY-MAC on 15-6-8. // Copyright (c) 2015年 www.train.com. All rights reserved. // 字典转模型 #import <UIKit/UIKit.h> @interface YJAppInfo : UIView @property(nonatomic,copy)NSString *name; @property(nonatomic,copy)NSString *icon; @property(nonatomic,strong,readonly)UIImage *image; - (instancetype)initWithDict:(NSDictionary *)dict; /** 类方法可以快速实例化一个对象 */ + (instancetype)appInfoWithDict:(NSDictionary *)dict; +(NSArray *)appList; @end // // YJAppInfo.m // 05-应用程序管理的练习 // // Created by JACKY-MAC on 15-6-8. // Copyright (c) 2015年 www.train.com. All rights reserved. // #import "YJAppInfo.h" @implementation YJAppInfo @synthesize image = _image; - (UIImage *)image { if (_image==nil) { _image = [UIImage imageNamed:self.icon]; } return _image; } - (instancetype)initWithDict:(NSDictionary *)dict { self = [super init]; if (self) { // 用字典给属性赋值,所有与plist键值有关的方法,均在此处! // self.name = dict[@"name"]; //self.icon = dict[@"icon"]; [self setValuesForKeysWithDictionary:dict]; } return self; } + (instancetype)appInfoWithDict:(NSDictionary *)dict { return [[self alloc] initWithDict:dict]; } +(NSArray *)appList { NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil]]; // 创建一个临时数组 NSMutableArray *arrayM = [NSMutableArray array]; // 遍历数组,依次转换模型 for (NSDictionary *dict in array) { YJAppInfo *appInfo = [YJAppInfo appInfoWithDict:dict]; [arrayM addObject:appInfo]; } return arrayM; } @end // // YJAppView.h // 05-应用程序管理的练习 // // Created by JACKY-MAC on 15-6-9. // Copyright (c) 2015年 www.train.com. All rights reserved. // 封装子控件view #import <UIKit/UIKit.h> @class YJAppInfo; @interface YJAppView : UIView /** 类方法,方便调用视图 */ + (instancetype)appView; /** 实例化视图,并使用appInfo设置视图的显示 */ + (instancetype)appViewWithAppInfo:(YJAppInfo *)appInfo; //@property (weak, nonatomic) IBOutlet UIButton *button; // 自定义视图中显示的数据来源是数据模型 // 使用模型设置自定义视图的显示 @property(nonatomic,strong)YJAppInfo *appInfo; @end // // YJAppView.m // 05-应用程序管理的练习 // // Created by JACKY-MAC on 15-6-9. // Copyright (c) 2015年 www.train.com. All rights reserved. // #import "YJAppView.h" #import "YJAppInfo.h" @interface YJAppView () @property (weak, nonatomic) IBOutlet UIImageView *iconView; @property (weak, nonatomic) IBOutlet UILabel *label; @end @implementation YJAppView // 从XIB来加载自定义视图 + (instancetype)appView { return [[[NSBundle mainBundle] loadNibNamed:@"YJAppView" owner:nil options:nil] lastObject]; } + (instancetype)appViewWithAppInfo:(YJAppInfo *)appInfo { // 1. 实例化一个视图 YJAppView *view = [self appView]; // 2. 设置视图的显示 view.appInfo =appInfo; // 3. 返回视图 return view; } - (void)setAppInfo:(YJAppInfo *)appInfo { _appInfo = appInfo; self.label.text = appInfo.name; self.iconView.image = appInfo.image; } - (IBAction)click:(UIButton *)button { // 取出appInfo // YJAppInfo *appInfo = self.appList[button.tag]; // 添加一个UILabel到界面上 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(80, 400, 160, 40)]; label.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.2]; label.text = [NSString stringWithFormat:@"成功安装%@",self.appInfo.name]; label.textAlignment = NSTextAlignmentCenter; label.font = [UIFont systemFontOfSize:12.0]; [self.superview addSubview:label]; // 动画效果 // 收尾式动画,修改对象的属性,frame,bounds,alpha // 初始透明度,完全透明 label.alpha = 0.0; button.enabled = NO; [button setTitle:@"下载完成" forState:UIControlStateNormal]; // 动画结束之后删除 // ^ 表示是block,块代码,是一个预先准备好的代码块,可以当做参数传递,在需要的时候执行! // 块代码在OC中,使用的非常普遍! [UIView animateWithDuration:1.0f animations:^{ // 要修改的动画属性 label.alpha = 1.0; } completion:^(BOOL finished) { [UIView animateWithDuration:3.0 animations:^{ label.alpha = 0.0; } completion:^(BOOL finished) { [label removeFromSuperview]; }]; }]; } @end // // YJViewController.m // 05-应用程序管理的练习 // // Created by JACKY-MAC on 15-6-5. // Copyright (c) 2015年 www.train.com. All rights reserved. //主控制器 #import "YJViewController.h" #import "YJAppInfo.h" #import "YJAppView.h" #define kAppViewW 80 #define kAppViewH 90 #define kColCount 3 #define kStartY 20 @interface YJViewController () @property(nonatomic,strong)NSArray *appList; @end @implementation YJViewController - (NSArray *)appList { if (_appList == nil) { //_appList = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil]]; // 将临时数组为属性赋值 _appList = [YJAppInfo appList]; } return _appList; } - (void)viewDidLoad { [super viewDidLoad]; CGFloat marginX = (self.view.bounds.size.width - kColCount * kAppViewW) / (kColCount + 1); CGFloat marginY = 10; for (int i = 0; i < self.appList.count; i++) { // 行 横为行 // 0, 1, 2 => 0 // 3, 4, 5 => 1 int row = i /kColCount; // 列 竖为列 // 0, 3, 6 => 0 // 1, 4, 7 => 1 // 2, 5, 8 => 2 int col = i % kColCount; CGFloat x = marginX + col * (marginX + kAppViewW); CGFloat y =kStartY + marginY +row * (marginY + kAppViewH); // 实现视图内部细节 YJAppView *appView = [YJAppView appViewWithAppInfo:self.appList[i]]; appView.frame = CGRectMake(x, y, kAppViewW, kAppViewH); [self.view addSubview:appView]; @end
时间: 2024-10-14 10:24:32