控件加载图片,plist,懒加载,序列帧动画,添加动画效果。
IOS中有2种加载图片的方式、
方式一:有缓存(图片所占用的内存会一直停留在程序中)
+ (UIImage *)imageNamed:(NSString *)name;注:必须是png文件。需要把图片添加到 images.xcassets中
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView; self.iconImageView.image=[UIImage imageNamed:@"icon"];
+ (UIImage *)imageWithContentsOfFile:(NSString *)path - (id)initWithContentsOfFile:(NSString *)path;
/* 不分组方式来使用图片 文件夹颜色为黄色。路径为 mainBundle/图片名字.后缀*/ NSString *imgName=@"icon.jpg"; // NSString *imgpath=[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:imgName]; // 与下面这句效果相同 NSString *imgpath=[[NSBundle mainBundle] pathForResource:imgName ofType:nil]; UIImage *image=[UIImage imageWithContentsOfFile:imgpath]; /* 分组方式来使用图片 文件夹颜色为蓝色色。路径为 mainBundle/图片所在路径/图片名字.后缀*/ // 使用另外一种方式来读取图片 NSString *bundlePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Animations副本"]; NSString *animPath = [bundlePath stringByAppendingPathComponent:imgName]; UIImage *image = [UIImage imageWithContentsOfFile:animPath];
plist:
一般可以使用属性列表文件存储NSArray或者NSDictionary之类的数据,这种属性列表文件的扩展名是plist,因此也成为“Plist文件”
// 获得Plist文件的全路径 NSBundle *bundle = [NSBundle mainBundle]; NSString *path = [bundle pathForResource:@"plistName" ofType:@"plist"]; //也可以 //NSString *path = [bundle pathForResource:@"plistName.plist" ofType:nil];
NSArray pList=[NSArray arrayWithContentsOfFile:path];
LFAppInfo.m #import "LFAppInfo.h" @implementation LFAppInfo -(instancetype)initWithPlist:(NSDictionary *)dict{ self.icon=dict[@"icon"]; self.name=dict[@"name"]; return self; } +(instancetype)appInfoWithPlist:(NSDictionary *)dict{ return [[self alloc] initWithPlist:dict]; } @end
LFAppInfo.m #import "LFAppInfo.h" @implementation LFAppInfo -(instancetype)initWithPlist:(NSDictionary *)dict{ self.icon=dict[@"icon"]; self.name=dict[@"name"]; return self; } +(instancetype)appInfoWithPlist:(NSDictionary *)dict{ return [[self alloc] initWithPlist:dict]; } @end
- (NSArray *)images { if (_images == nil) { NSBundle *bundle = [NSBundle mainBundle]; NSString *path = [bundle pathForResource:@"imageData" ofType:@"plist"]; _images = [NSArray arrayWithContentsOfFile:path]; } return _images; }
-(void)tomAnimation:(NSString *)img count:(int)count{ if([self.tom isAnimating]) return; NSMutableArray *arrayImg=[NSMutableArray array]; for(int i=0;i<count;i++){ NSString *imgName=[NSString stringWithFormat:@"%@_%02d.jpg",img,i]; // NSString *imgpath=[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:imgName]; // 与下面这句效果相同 NSString *imgpath=[[NSBundle mainBundle] pathForResource:imgName ofType:nil]; UIImage *image=[UIImage imageWithContentsOfFile:imgpath]; [arrayImg addObject:image]; } [self.tom setAnimationImages:arrayImg]; [self.tom setAnimationRepeatCount:1]; [self.tom setAnimationDuration:arrayImg.count*0.075]; [self.tom startAnimating]; [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration]; }
1.block方式(一般都使用这种方式)
[UIView animateWithDuration:duration delay:0.0 options:7 << 16 animations:^{ // 需要执行动画的代码 } completion:^(BOOL finished) { // 动画执行完毕执行的代码 }];
2. 普通方法
[UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>// 需要执行动画的代码 <span style="white-space:pre"> </span> [UIView commitAnimations];