標籤:
//// 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
iOS學習 - 應用程式管理