IOS的三种动画:
1 UIImage 实现动画
2 UIImageView 动画
可以通过 isAnimating 判断动画是否正在运行
3 UIView动画
最简单的UIImage动画,输入名字会自动去找 0 1 2的系列图片
参考官方文档说明:
“This method loads a series of files by appending a series of numbers to the base file name provided in the name”
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *image = [UIImage animatedImageNamed:@"ship-anim" duration:1];
self.imageView.image = image;
}
UIimage View动画
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//UIImageView 动画
NSMutableArray *mArray = [[NSMutableArray alloc]init];
for (NSInteger i = 0; i < 40; i++) {
//创建动画 每帧需要的 图片对象
//UIImage *roleImage = [UIImage imageNamed:[NSString stringWithFormat:@"shaoNv3_%02ld",i+1]];
//创建动画 每帧需要的 图片对象
NSString *path = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"shaoNv3_%02ld",i+1] ofType:@"png"];
NSLog(@"文件 路径 i=%ld %@",i , path);
//file参数要的 文件的路径
UIImage *roleImage = [UIImage imageWithContentsOfFile:path];
[mArray addObject:roleImage];
}
//设置动画持续时间
self.imageView2.animationDuration = 3;
//设置动画播放次数 如果值为0的话 表示 无线循环
self.imageView2.animationRepeatCount = 3;
//将动画对象数组 赋值给 imageView 的 animatingImages 属性
self.imageView2.animationImages = mArray;
//运行动画
[self.imageView2 startAnimating];
//多长时间后 做什么事
[self performSelector:@selector(removeImage) withObject:nil afterDelay:self.imageView2.animationDuration * self.imageView2.animationRepeatCount];
}
最常用的UIView动画:
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
myView.alpha = 0;
myView.backgroundColor = [UIColor greenColor];
[self.view addSubview:myView];
[UIView animateWithDuration:3 animations:^{
// CGRect frame = myView.frame;
// frame.size = CGSizeMake(300, 300);
// myView.frame = frame;
CGRect bounds = myView.bounds;
bounds.size = CGSizeMake(300, 300);
myView.bounds = bounds;
myView.alpha = 1;
}];
// [UIView animateWithDuration:<#(NSTimeInterval)#> animations:<#^(void)animations#> completion:<#^(BOOL finished)completion#>]
// [UIView animateWithDuration:持续时间 delay:等待时间 options:动画选项 animations:<#^(void)animations#> completion:<#^(BOOL finished)completion#>]
}