[UIImage animatedImageWithImages:**动画图片数组** duration:**持续时间**]; // 可以获取一个能做动画的UIImage对象"gif图片"
self.imageView.animationImages = array; // 装图片的数组(需要做动画的图片数组)
self.imageView.animationDuration = 2; // 动画时间
self.imageView.animationRepeatCount = 1; // 重复次数 0 表示重复
[self.imageView startAnimating]; // 开始序列帧动画
NSString *path = [[NSBundle mainBundle] pathForResource:@"images.plist" ofType:nil];
NSArray *images = [NSArray arrayWithContentsOfFile:path];
- NSURL *url = [[NSBundle mainBundle] URLForResource:@"images.plist" withExtension:nil];
- NSArray *images = [NSArray arrayWithContentsOfURL:url];
self.imageView.image = [UIImage imageNamed:@"image文件名"];
[UIImage imageWithContentsOfFile:<#(nonnull NSString *)#>];
从 NSBundle加载XIB,只需要在第一个参数传入 XIB 的文件名,注意:没有扩展名
UIView *appView = [[NSBundle mainBundle] loadNibNamed:@"CZAppView" owner:nil options:nil].lastObject;
//第一个参数:XIB的名字
//第二个参数:Bundle 名,如果传入 nil,会自动从 mainBundle 获取
UINib *nib = [UINib nibWithNibName:@"CZAppView" bundle:nil];
//第一个、第二个参数,老师没有讲,说自己从来没有用过。就写nil。
UIView *appView = [nib instantiateWithOwner:nil options:nil].lastObject;
管理xib的类一般做两个事件
#import <UIKit/UIKit.h>
@class HMApp;
@interface HMAppView : UIView
// 定义一个属性用来接收数据
@property (nonatomic, strong) HMApp *app;
// 创建appView
+ (instancetype)appView;
@end
// 定义格子的宽和高
NSInteger appW = 100;
NSInteger appH = 120;
// 定义格子的总数量
NSInteger countOfApps = 80;
// 定义一行有几个格子
NSInteger numberOfCol = 3;
// 定义左边距、右边距、顶部间距
NSInteger marginOfLeft = 10;
NSInteger marginOfRight = 10;
NSInteger marginOfTop = 30;
// 格子之间的间距 视图总宽度-左边距-右边距-(格子宽*一行有几个) / (一行有几个 减 1)
NSInteger marginOfApp = (self.view.bounds.size.width - marginOfLeft - marginOfRight - (appW * numberOfCol)) / (numberOfCol - 1);
// 开始循环布局
for (NSInteger i = 0; i < countOfApps; i++) {
//计算列号 第几个格子的索引 % 每行的个数
NSUInteger col = i % numberOfCol;
// 计算X = 左边间距 + (格子的宽 + 格子之间的间距) * 列号
CGFloat appX = marginOfLeft + (appW + marginOfApp)*col;
//计算行号 行号 = i / 列数
NSUInteger row = i / numberOfCol;
// 计算格子的Y = 顶部间距 + (格子的高 + 格子之间间距) * 行号
CGFloat appY = marginOfTop + (appH + marginOfApp)*row;
// 建立UIView
GMAppView *xib = [GMAppView GMAppView];
xib.appInfo = _appData[i];
//设置frame
xib.frame = CGRectMake(appX, appY, appW, appH);
//把appView添加到控制器的view上
[self.view addSubview:xib];
}
dict[@"name"] = @"Jack";NSString *name = dict[@"name"];```
##8.2 字典转模型的流程
![](http://upload-images.jianshu.io/upload_images/2248583-c479385a9d8363d9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
##8.3 注意点
模型应该提供一个可以传入字典参数的构造方法
**同时,还要提供读取Plist,并转为数组的方法**
-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)xxxWithDict:(NSDictionary *)dict;
//方法实现
}
return [[self alloc]initWithDict:dictionary];
}
/**
return a array from Plist
@return NSArray
*/
// 获取plist文件的NSURL
NSURL * plistUrl = [[NSBundle mainBundle] URLForResource:@"apps.plist" withExtension:nil];
// 通过Plist 的URL读出文件中的array。
NSArray * array = [NSArray arrayWithContentsOfURL:plistUrl];
NSMutableArray * arrayM = [NSMutableArray array];
// plist中的数组中存放的是一个一个字典,因此遍历数组中的字典。把遍历出来的字典添加入可变数组。
for (NSDictionary * dict in array) {
GMModel * temp = [GMModel modelWithDict:dict];
[arrayM addObject:temp];
}
return arrayM;
}
提示:在模型中合理地使用只读属性,可以进一步降低代码的耦合度。
声明属性
@property (nonatomic, copy) NSString *name;@property (nonatomic, copy) NSString *icon;
// 图像,定义属性时,会生成getter&setter方法,还会生成一个带下划线的成员变量// 如果是readonly属性,只会生成getter方法,同时没有成员变量@property (nonatomic, strong, readonly) UIImage *image;
### 8.3.1 作为模型
- 有和字典一样的属性
- 有一个根据字典返回自己的方法 (类方法、对象方法两种)
- 加载plist并转为数组的方法
### 8.3.2 作为view
- 有一个返回自己的实例的方法(至于怎么创建的 无所谓)
- 在.h 里面有一个用来接收模型类型的属性
- 在.m 里面应该有需要显示值的子控件
- 重写set模型的方法,先模拟系统的_xx=xx,把数据放在子控件当中
### 8.3.3 作为controller
- 解析数据,注意转化成模型.步骤(路径,解析临时数组,创建可变数组,遍历获取字典,字典转模型,把模型添加到可变数组中,返回).
- 根据view提供的方法添加到视图上(c不关心view具体是如何创建的),并且,把解析的模型数据复制给view.h里面用来接收模型类型的属性