一.使用MVC
1.模型文件如下
// MJApp.h
#import <Foundation/Foundation.h>
/**
copy : NSString
strong: 一般對象
weak: UI控件
assign:基本數據類型
*/
@interface MJApp : NSObject
/**
* 名稱
*/
@property (nonatomic, copy) NSString *name;
/**
* 圖標
*/
@property (nonatomic, copy) NSString *icon;
/**
* 通過字典來初始化模型對象
*
* @param dict 字典對象
*
* @return 已經初始化完畢的模型對象
*/
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)appWithDict:(NSDictionary *)dict;
@end
// MJApp.m
#import "MJApp.h"
@implementation MJApp
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
self.name = dict[@"name"];
self.icon = dict[@"icon"];
}
return self;
}
+ (instancetype)appWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
@end
如下圖
// MJAppView.h
#import <UIKit/UIKit.h>
@class MJApp, MJAppView;
// 聲明一個協議
@protocol MJAppViewDelegate <NSObject>
@optional
- (void)appViewClickedDownloadButton:(MJAppView *)appView;
@end
// 耦合性
@interface MJAppView : UIView
/**
* 代理
*/
@property (nonatomic, weak) id<MJAppViewDelegate> delegate;
//@property (weak, nonatomic) IBOutlet UIButton *downloadBtn;
//@property (nonatomic, weak) UIView *vcView;
/**
* 模型數據
*/
@property (nonatomic, strong) MJApp *app;
+ (instancetype)appView;
/**
* 通過模型數據來創建一個view
*/
+ (instancetype)appViewWithApp:(MJApp *)app;
@end
連線到.m文件中// MJAppView.m
#import "MJAppView.h"
#import "MJApp.h"
@interface MJAppView()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
- (IBAction)download:(UIButton *)sender;
@end
@implementation MJAppView
+ (instancetype)appViewWithApp:(MJApp *)app
{
NSBundle *bundle = [NSBundle mainBundle];
// 讀取xib文件(會創建xib中的描述的所有對象,並且按順序放到數組中返回)
NSArray *objs = [bundle loadNibNamed:@"MJAppView" owner:nil options:nil];
MJAppView *appView = [objs lastObject];
appView.app = app;
return appView;
}
+ (instancetype)appView
{
return [self appViewWithApp:nil];
}
- (void)setApp:(MJApp *)app
{
_app = app;
// 1.設置圖標
self.iconView.image = [UIImage imageNamed:app.icon];
// 2.設置名稱
self.nameLabel.text = app.name;
}
/**
* 下載
*/
- (IBAction)download:(UIButton *)btn {
// 1.讓按鈕失效(文字變爲"已下載")
btn.enabled = NO;
// 2.通知代理
// [self.delegate appViewClickedDownloadButton:self];
if ([self.delegate respondsToSelector:@selector(appViewClickedDownloadButton:)]) {
[self.delegate appViewClickedDownloadButton:self];
}
}
@end
3.MJViewController.m代碼如下
// MJViewController.m
#import "MJViewController.h"
#import "MJApp.h"
#import "MJAppView.h"
@interface MJViewController () <MJAppViewDelegate>
/** 存放應用信息 */
@property (nonatomic, strong) NSArray *apps;
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 添加應用信息
// 0.總列數(一行最多3列)
int totalColumns = 3;
// 1.應用的尺寸
CGFloat appW = 85;
CGFloat appH = 90;
// 2.間隙 = (控制器view的寬度 - 3 * 應用寬度) / 4
CGFloat marginX = (self.view.frame.size.width - totalColumns * appW) / (totalColumns + 1);
CGFloat marginY = 15;
// 3.根據應用個數創建對應的框框(index 0 ~ 11)
for (int index = 0; index<self.apps.count; index++) {
// 3.1.創建view
MJAppView *appView = [MJAppView appViewWithApp:self.apps[index]];
appView.delegate = self;
// appView.downloadBtn.tag = index;
// [appView.downloadBtn addTarget:self action:@selector(download:) forControlEvents:UIControlEventTouchUpInside];
// appView.vcView = self.view;
// 3.2.添加view
[self.view addSubview:appView];
// 3.3.設置frame
int row = index / totalColumns;
int col = index % totalColumns;
// 計算x和y
CGFloat appX = marginX + col * (appW + marginX);
CGFloat appY = 30 + row * (appH + marginY);
appView.frame = CGRectMake(appX, appY, appW, appH);
// 3.4.設置數據
// appView.app = self.apps[index];
}
}
/**
* 當點擊下載按鈕時就會調用
*/
- (void)appViewClickedDownloadButton:(MJAppView *)appView
{
// 1.取出模型
MJApp *app = appView.app;
// 2.添加標籤
UILabel *label = [[UILabel alloc] init];
label.text = [NSString stringWithFormat:@"成功下載%@", app.name];
label.font = [UIFont systemFontOfSize:12];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor blackColor];
label.frame = CGRectMake(0, 0, 150, 25);
label.center = CGPointMake(160, 240);
label.alpha = 0.0;
label.layer.cornerRadius = 5;
label.clipsToBounds = YES;
[self.view addSubview:label];
// 3.動畫
[UIView animateWithDuration:1.0 animations:^{
label.alpha = 0.5;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
label.alpha = 0.0;
} completion:^(BOOL finished) {
[label removeFromSuperview];
}];
}];
}
- (NSArray *)apps
{
if (_apps == nil) {
// 初始化
// 1.獲得plist的全路徑
NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
// 2.加載數組
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
// 3.將dictArray裏面的所有字典轉成模型對象,放到新的數組中
NSMutableArray *appArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
// 3.1.創建模型對象
MJApp *app = [MJApp appWithDict:dict];
// 3.2.添加模型對象到數組中
[appArray addObject:app];
}
// 4.賦值
_apps = appArray;
}
return _apps;
}
@end
效果圖如下: