UIImageView
1.定义:它是动画的类,可以产生动画
2.初始化:
UIImageView *view = [[UIImageView alloc]initWithFrame:CGRectMake(*,*,*,*)];
3.属性:
1⃣️:view.animationDuration = 1; 设置完成整个动画的时间
2⃣️:view.animationRepeatCount = -1; 设置动画执行次数(-1无线重复循环,1代表依次,2两次)
3⃣️:view.animationImages = images; 设置动画的图片 (图片来源于一个可变数组)
4⃣️:[view startAnimating]; 开始动画
5⃣️:[view stopAnimating]; 停止动画
6⃣️:[view.isAnimating = YES]; 判断动画是否在播放
7⃣️:在动画执行的时候,改变动画的位置:
[UIView animateWithDuration:5 animations:^{
animationView.frame = CGRectMake(175, 400, 100*2, 100*2);
} completion:^(BOOL finished) {
[UIView animateWithDuration:5 animations:^{
animationView.frame = CGRectMake(0, 0, 100, 100);
}];
}];
这里是嵌套了一层,让动画执行完过后,回到原来的位置
心得:
1.往可变数组里面加载图片的时候,使用for循环加载图片,需将图片按顺序命名,图片命名从哪个数字开始,for循环就从哪个数字开始。
2.当有很多组图片的时候,如果要加载,可以设置两个参数(图片名字只有前缀和个数不一样):
- (void)animationWithCount:(int)count andname:(NSString *)name
{
if (tommy.isAnimating ) {
return;
这是判断当前动画是否在执行
}
NSMutableArray *images = [NSMutableArray array];
for (int i = 0; i < count; i ++) {
NSString *fileName = [NSString stringWithFormat:@"%@_%02d.jpg",name,i];
NSBundle *myBoundle = [NSBundle mainBundle];
NSString *path = [myBoundle pathForResource:fileName ofType:nil];
NSBundle 找全局文件的类名
// UIImage *image = [[UIImage alloc]init];
// image = [UIImage imageWithContentsOfFile:path];
UIImage *image = [[UIImage alloc]initWithContentsOfFile:path];
initWithContentsOfFile:在程序内没有缓存 imageNamed:在程序内有缓存
[images addObject:image];
}
tommy.animationImages = images;
tommy.animationRepeatCount = 1;
tommy.animationDuration = images.count*0.1;
[tommy startAnimating];
CGFloat delay = tommy.animationDuration + 0.5;
[tommy performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];
在动画执行完0.5秒过后清除内存
}
3.在初始化动画的时候,一定要给动画执行的位置,不然它不知道在哪播放。
4.用for循环加载的图片的时候一定要仔细,不要重复在for循环内部加载动画。
5.在viewDidLoad里面,要想清楚程序的执行顺序,错误的执行顺序会导致程序无法运行。