使用UIView和CALayer都能实现动画效果,但是在真实的开发中,一般还是主要使用UIView封装的动画,而很少使用CALayer的动画。
CALayer核心动画与UIView动画的区别:
UIView封装的动画执行完毕之后不会反弹。即如果是通过CALayer核心动画改变layer的位置状态,表面上看虽然已经改变了,但是实际上它的位置是没有改变的(方法一:需要加入代码animation.removedOnCompletion = NO;和animation.fillMode = kCAFillModeForwards,保持动画结束后就停在当前状态而不回到原来的状态。方法二:在加动画的时候只设置fromValue,不设置toValue,在开始时就将view的位置为toValue的值。)。
一.基本方式:使用UIView类的UIViewAnimation扩展
-(void)animation:(UIButton *)button{
NSInteger tag = button.tag;
//创建一个CGAffineTransform transform对象
CGAffineTransform transformRotate;
CGAffineTransform transformScale;
CGAffineTransform transformfan;
switch (tag) {
case 1://翻转动画
[UIView beginAnimations:@"animation" context:nil];
//设置时常
[UIView setAnimationDuration:1];
/**
*
UIViewAnimationCurveEaseInOut,
UIViewAnimationCurveEaseIn,
UIViewAnimationCurveEaseOut,
UIViewAnimationCurveLinear
*/
//设置动画淡入淡出
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//设置代理
[UIView setAnimationDelegate:self];
//设置翻转方向
[UIView setAnimationTransition:
UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
//动画结束
[UIView commitAnimations];
break;
case 2://旋转动画
//设置旋转度数
transformRotate = CGAffineTransformRotate(self.view.transform,M_PI/2.0);
//动画开始
[UIView beginAnimations:@"rotate" context:nil ];
//动画时常
[UIView setAnimationDuration:2];
//添加代理
[UIView setAnimationDelegate:self];
//获取transform的值
[self.view setTransform:transformRotate];
//关闭动画
[UIView commitAnimations];
break;
case 3://偏移动画
[UIView beginAnimations:@"move" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
//改变它的frame的x,y的值
self.view.frame=CGRectMake(100,100, 120,100);
[UIView commitAnimations];
break;
case 4://翻页动画
[UIView beginAnimations:@"curlUp" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//指定动画曲线类型,该枚举是默认的,线性的是匀速的
//设置动画时常
[UIView setAnimationDuration:1];
[UIView setAnimationDelegate:self];
//设置翻页的方向
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
//关闭动画
[UIView commitAnimations];
break;
case 5://翻页动画 并且交换两个视图 使其看起来像是两页
[UIView beginAnimations:@"curlUp" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//指定动画曲线类型,该枚举是默认的,线性的是匀速的
//设置动画时常
[UIView setAnimationDuration:1];
[UIView setAnimationDelegate:self];
//设置翻页的方向
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
// 交换本视图控制器中2个view位置
[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];//比按钮4增加了这一行代码 特别注意这个代码的实现,必须将main.storyboard去掉,然后再appledelegate中重新定义这个控制器为根视图控制器才可以,否则这个代码不起作用
//关闭动画
[UIView commitAnimations];
break;
case 6://缩放动画
transformScale = CGAffineTransformScale(self.view.transform,1.2,1.2);
[UIView beginAnimations:@"scale" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
[self.view setTransform:transformScale];
[UIView commitAnimations];
break;
case 7://取反的动画效果是根据当前的动画取他的相反的动画
transformfan=CGAffineTransformInvert(self.view.transform);
[UIView beginAnimations:@"Invert" context:nil];
[UIView setAnimationDuration:2];//动画时常
[UIView setAnimationDelegate:self];
[self.view setTransform:transformfan];//获取改变后的view的transform
[UIView commitAnimations];//关闭动画
break;
default:
break;
}
}
二 block方式:使用UIView类的UIViewAnimationWithBlocks扩展
- (void)changeUIView{
////间隔,延迟,动画参数,界面更改块,结束块
[UIView animateWithDuration:2
delay:0
options:UIViewAnimationOptionCurveEaseOut animations:^(void){
//界面更改块
_blueView.alpha = 0.0;
}completion:^(BOOL finished){
//结束块
_blueView.alpha = 1.0;
}];
}
1251

被折叠的 条评论
为什么被折叠?



