说明:
UIView动画:
UIView动画,真实改变属性的值,类似android属性动画。
帧动画:
帧动画,一张张图片组合播放。类试电影效果。
使用:
一、UIView动画:
1.执行动画:
(1)头尾式:
/*
1.头尾式
*/
- (void)startEndAnim{
//开启动画
[UIView beginAnimations:nil context:nil];
//设置时间
[UIView setAnimationDuration:1.0];
//实现代理
[UIView setAnimationDelegate:self];
//动画完成后调用自定义方法
[UIView setAnimationDidStopSelector:@selector(onDone)];
//添加改变属性值的代码,真实改变值
self.v.center = CGPointMake(200, 400);
//执行动画
[UIView commitAnimations];
}
- (void)onDone{
NSLog(@"动画完成");
}
(2)Block方式:
/*
2.Block方式
*/
- (void)blockAnim{
//开启并执行动画
[UIView animateWithDuration:1.0 animations:^{
//添加改变属性值的代码,真实改变值
self.v.center = CGPointMake(200, 400);
} completion:^(BOOL finished) {
NSLog(@"动画完成");
}];
}
2.利用transform进行位移、缩放、旋转:
/*
利用transform进行位移、缩放、旋转
*/
- (void)tweenAnim{
//开启并执行动画
[UIView animateWithDuration:1.0 animations:^{
//平移:
//只平移一次,平移到距离原始位置XY差值的位置
self.v.transform = CGAffineTransformMakeTranslation(100, 400);
//基于指定的值再平移XY偏移量
self.v.transform = CGAffineTransformTranslate(self.v.transform, 100, 400);
//缩放:
//基于开始位置缩放
self.v.transform = CGAffineTransformMakeScale(0.5f, 0.5f);
//基于指定位置缩放
self.v.transform = CGAffineTransformScale(self.v.transform, 0.5f, 0.5);
//旋转:
//基于原值旋转,顺时针为正,逆时针为负
self.v.transform = CGAffineTransformMakeRotation(M_PI_2);
//移动+旋转效果,顺时针为正,逆时针为负
self.v.transform = CGAffineTransformRotate(self.v.transform, -M_PI_2);
} completion:^(BOOL finished) {
NSLog(@"动画完成");
//回到原位置原状态:
self.v.transform = CGAffineTransformIdentity;
}];
}
二、帧动画:
1.动态加载图片到NSArray中:
NSMutableArray *imgList = [NSMutableArray array]; //创建可变数组
for(int i = 0; i < 10; i++){
NSString *imgName = [NSString stringWithFormat:@"图片名称前缀_%02d.jpg", i]; //%02d表示遇到10以下的数字时以0开头(如:01),10及以上保持原值
//UIImage *img = [UIImage imageNamed:imgName]; //根据名称加载图片,这种方式图片不会释放
NSString *path = [NSBundle mainBundle pathForResource:imgName ofType:nil]; //获取图片路径
UIImage *img = [UIImage imageWithContentsOfFile:path]; //根据路径加载图片,这种方式图片会释放
[imgList addObject:imgName]; //将图片对象加入list中
}
2.设置图片:
self.imgViewCat.animation = imgList;
3.创建并播放动画:
//设置动画时间
self.imgViewCat.animationDuration = 持续秒数; //int类型的秒数值
//设置是否需要重复播放
self.imgViewCat.animationRepeatCount = 1;
//开启动画
[self.imgViewCat startAnimating];
//清空图片集合,在播放完后执行清空
[self.imgViewCat performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:1];
三、转场动画:
/*
转场动画
*/
- (void)anim{
//开启并执行动画
[UIView transitionWithView:self.v duration:1.0 options:UIViewAnimationOptionTransitionCurlUp animations:^{
//执行过程中
} completion:^(BOOL finished) {
//执行完成
}];
}