UIview简单动画
一 Core Animation
核心动画是分三层进行的-动画层树(CALayer Tree)M、展示树(Presentation Tree)、以及渲染树(Render Tree)。
每一个动画层有着自己的坐标系,这个坐标系独立于其父动画层。在iOS上,动画层的坐标系原点在左上角,坐标轴向右向下延伸。
动画有隐式和显示之分。隐式动画指的是,无须创建动画对象,只需改变动画层的属性,让核心动画自己去完成动画效果。显示动画指的是,需要自己创建和管理动画对象,并且将它们应用到动画层,才能显示动画效果。
UIImageView有animationImages属性,如果将一个UIImage对象构成的NSArray数组赋值给它,UIImageView将轮流显示这些图像。给UIImageView发送startAnimating消息,那么它就开始逐帧显示动画了。animationDuration属性标明了每一帧动画占用的时间,默认1/30秒。animationRepeatCount属性标明了动画重复次数,缺省为0,表示不断重复下去。
方法:
+beginAnimations:context:标志动画代码开始
+commitAnimations:标志动画代码结束,程序会创建新的线程,并准备运行动画
+setAnimationStartDate:设置动画开始时间。
+setAnimationsEnabled:可以用来开启或禁止动画显示。
+setAnimationDelegate:设置代理,可以接收到UIView的代理方法。
+setAnimationWillStartSelector:设置动画开始前将发送给代理的方法。
+setAnimationDidStopSelector:设置动画停止后将发送给代理的方法。
+setAnimationDuration:设置动画持续时间。
+setAnimationDelay:设置一段时间,动画将在这段时间后开始执行。不建议使用
+setAnimationCurve:设置动画曲线,如开始慢,后面快。
+setAnimationRepeatCount:设置动画重复次数。
+areAnimationEnabled:检查是否已经启动动画。
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.0];
CGAffineTransform newTransform = CGAffineTransformRotate(view.transform, 3.14/2);
view.trnsform = newTransform;
[UIView commitAnimations];
//简单移动
imageView.transform = CGAffineTransformIdentity;
imageView.frame=CGRectMake(0, 100, 320, 320);
[UIView beginAnimations:@"clearmemory"context:imageView];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(enablebutton)];
imageView.frame=CGRectMake(34, 0, 320, 320);
[UIView commitAnimations];
//动画曲线
[UIView beginAnimations:nilcontext:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
// UIViewAnimationCurveEaseInOut, // slow at beginning and end
//UIViewAnimationCurveEaseIn, // slow at beginning
//UIViewAnimationCurveEaseOut, // slow at end
//UIViewAnimationCurveLinear //恒定速度
[UIView setAnimationDidStopSelector:@selector(enablebutton:)];
imageView.frame=CGRectMake(22, 0, 320, 320);
[UIView commitAnimations];
//反向重复
[UIView beginAnimations:@"animation3" context:imageView1];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:1.0];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:10];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(enablebutton:)];
imageView1.alpha=0;
[UIView commitAnimations];
// 延时,缓入,缓出
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelay:0.5];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(enablebutton:)];
imageView.frame=CGRectMake(120, 0, 200, 200);
[UIView commitAnimations];
//缓出
[UIView beginAnimations:nilcontext:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(enablebutton:)];
imageView.frame =CGRectMake(235, 144, 200 , 200);
[UIView commitAnimations];
//放大
[UIView beginAnimations:nilcontext:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(enablebutton:)];
imageView.transform=CGAffineTransformMakeScale(2, 2);
[UIView commitAnimations];
//旋转放大这里用到179.9是因为你不管前面加-+都是逆时针
[UIView beginAnimations:nilcontext:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(enablebutton:)];
CGAffineTransform tranform1=CGAffineTransformMakeScale(1, 1);
CGAffineTransform tranform3=CGAffineTransformMakeTranslation(200, 200);
CGAffineTransform tranform2=CGAffineTransformMakeRotation(179.9*M_PI/180.0);
imageView.transform =CGAffineTransformConcat(tranform1, tranform3);
[UIView commitAnimations];
//平移旋转
[UIView beginAnimations:nilcontext:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(enablebutton:)];
CGAffineTransform tranform1=CGAffineTransformMakeTranslation(-200, 0);
// CGAffineTransform tranform2= CGAffineTransformMakeRotation(179.9*M_PI/180.0);
imageView.transform =CGAffineTransformRotate(tranform1, 359.9*M_PI/180.0);
[UIView commitAnimations];
//翻转
[UIView beginAnimations:nilcontext:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRightforView:self.viewcache:YES];
//UIViewAnimationTransitionFlipFromLeft 从左往右翻
//UIViewAnimationTransitionFlipFromRight从右往左翻
//UIViewAnimationTransitionCurlUp 从上往下翻
//UIViewAnimationTransitionCurlDown 从下往上翻
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(enablebutton:)];
imageView.hidden=YES;
imageView1.hidden=NO;
[UIView commitAnimations];
//淡入淡出
CATransition *animation = [CATransition animation];
animation.duration = 0.75f; //动画时长
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
animation.delegate = self;
animation.type = kCATransitionFade; //过度效果
//kCATransitionFade 淡入淡出
//kCATransitionMoveIn 移入移出
//kCATransitionPush 压入推出
//kCATransitionReveal 覆盖移除
animation.subtype=kCATransitionFromLeft;
//kCATransitionFromRight 从左
//kCATransitionFromLeft 从右
//kCATransitionFromTop 从上
//kCATransitionFromBottom 从下
[self.view.layer addAnimation:animation forKey:@"animation"];
imageView.hidden=YES;
imageView1.hidden=NO;
-(void)enablebutton:(id)sender
{
imageView.transform=CGAffineTransformIdentity;
imageView.frame=CGRectMake(0, 0, 200, 200);
//btn.enabled=NO;
}