实现iphone的动画效果主要有两种方法,一种是UIView层面的,一种是使用CATransition进行更低层次的控制.
1、UIView
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:1.0]; //动画持续的时间
//这里添加你对UIView所做改变的代码
//[UIView setAnimationDidStopSelector:@selector(animationFinished:)]; //动画停止后,执行某个方法
[UIView commitAnimations];
2、UIView(使用CocoaTouch)
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
// Cocoa Touch
[UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView:myView cache:YES];
[UIView setAnimationDelegate:self];
//[UIView setAnimationDidStopSelector:@selector(animationFinished:)]; //动画停止后,执行某个方法
[UIView commitAnimations];
动画方式(UIViewAnimationTransition):
UIViewAnimationTransitionFlipFromLeft //从左向右翻转
UIViewAnimationTransitionFlipFromRight //从右向左翻转
UIViewAnimationTransitionCurlUp //从下向上翻页
UIViewAnimationTransitionCurlDown //从上向下翻页
3、CATransition
头部导入:
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = 1.0f; //动画执行时间
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.type = kCATransitionFade;
animation.subtype = kCATransitionFromRight;
// 这里添加你对UIView所做改变的代码
[[myView layer] addAnimation:animation forKey:@"animation"];
setType:有四种类型:
kCATransitionFade //交叉淡化过渡
kCATransitionMoveIn //移动覆盖原图
kCATransitionPush //新视图将旧视图推出去
kCATransitionReveal //底部显出来
setSubtype:有四种类型:
kCATransitionFromRight;
kCATransitionFromLeft(默认值)
kCATransitionFromTop;
kCATransitionFromBottom
注:kCATransitionFade 不支持Subtype
4、CATransition(只使用setType,参数是NSString)
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = 1.0f; //动画执行时间
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.type = @"suckEffect";// 这里添加你对UIView所做改变的代码
[[myView layer] addAnimation:animation forKey:@"animation"];
可以用的效果主要有:
pageCurl //向上翻一页
pageUnCurl //向下翻一页
rippleEffect //滴水效果
suckEffect //收缩效果,如一块布被抽走
cube //立方体效果
oglFlip //上下翻转效果