这是个非常简单的小例子。主要涉及知识点如下:
1. 按照plist中的字典创建模型
2. 使用Xib创建appView中的子控件, 并显示内容
3. 主控制器3步业务逻辑
1)懒加载创建appView数组
2)九宫格算法布局控件
3)控制器中实现appView 中下载按钮点击的代理方法
实例样子:
1. 按照plist中的字典创建模型
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface AppInfo : NSObject
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *icon;
//there is only a getter if 'readonly' is assigned
@property (nonatomic,strong,readonly) UIImage *image;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)appInfoWithDict:(NSDictionary *)dict;
@end
这里使用了KVC,直接从字典生成想要的对象
setValuesForKeysWithDictionary
#import "AppInfo.h"
@implementation AppInfo
{
UIImage *_image;
}
- (instancetype)initWithDict:(NSDictionary *)dict{
if(self = [super init]){
// input data using kvc
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (instancetype)appInfoWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];
}
- (UIImage *)image{
if(!_image){
_image = [UIImage imageNamed:self.icon];
}
return _image;
}
@end
懒加载创建appView数组
//Using lazy-load to create array
//override the getter of appViews
- (NSArray *)appViews{
if (!_appViews) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"app" ofType:@"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:array.count];
for (NSDictionary *dict in array) {
//create model instance
AppInfo *appInfo = [AppInfo appInfoWithDict:dict];
//create own view
AppView *appView = [AppView appView];
appView.appInfo = appInfo;
appView.delegate = self;
//add own view to mutable array
[arrayM addObject:appView];
}
_appViews = [arrayM copy];
}
return _appViews;
}
九宫格计算
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGFloat margin = (kScreenW - kTotalCol * kAppViewW)/(kTotalCol + 1);
for (int i=0; i < self.appViews.count; i++) {
AppView *appView = self.appViews[i];
//calcute the row and column number
int col = i % kTotalCol;
int row = i / kTotalCol;
//calcute the center point of every views
CGFloat centerX = (margin + kAppViewW * 0.5) + (margin + kAppViewW) * col;
CGFloat centerY = (margin + kAppViewH * 0.5) + (margin + kAppViewH) * row;
appView.center = CGPointMake(centerX, centerY);
[self.view addSubview:appView];
}
}
控制器中实现appView 中下载按钮点击的代理方法
#pragma mark - AppViewDelegate
- (void)downloadClickWithBtn:(UIButton *)btn{
//get the name of app
//Force convert
AppView *appView = (AppView *)btn.superview;
NSString *appName = appView.appInfo.name;
//generate the busy indicator
UIActivityIndicatorView *busy = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
busy.frame = self.view.bounds;
//busy.hidesWhenStopped = YES; //by default, it is YES
//busy.backgroundColor = [UIColor greenColor];
[busy startAnimating];
[self.view addSubview:busy];
// generate a lable for downloading above on the busy indicator
UILabel *downloadLable = [[UILabel alloc] init];
downloadLable.frame = CGRectMake(0, kScreenH * 0.5 + 10, kScreenW, 20);
downloadLable.textColor = [UIColor whiteColor];
downloadLable.textAlignment = NSTextAlignmentCenter;
downloadLable.text = [NSString stringWithFormat:@"%@正在下载。。。", appName];
downloadLable.font = [UIFont systemFontOfSize:15.0];
downloadLable.backgroundColor = [UIColor blackColor];
downloadLable.alpha = 0.5;
[busy addSubview:downloadLable];
//using GCD to
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[busy stopAnimating];
//the alert when download is done
UILabel *noteLable = [[UILabel alloc] init];
noteLable.frame = CGRectMake(0, kScreenH * 0.5 + 20, kScreenW, 30);
noteLable.textColor = [UIColor whiteColor];
noteLable.textAlignment = NSTextAlignmentCenter;
noteLable.text = [NSString stringWithFormat:@"%@下载完成", appName];
noteLable.font = [UIFont systemFontOfSize:15.0];
noteLable.backgroundColor = [UIColor blackColor];
noteLable.alpha = 1;
[self.view addSubview:noteLable];
//animation by block
[UIView animateWithDuration:2.0 animations:^{
//do the animation
noteLable.alpha = 0;
} completion:^(BOOL finished) {
//what we should do after the animation
btn.enabled = NO;
[btn setTitle:@"已下载" forState:UIControlStateDisabled];
[noteLable removeFromSuperview];
}];
});
}