UIView 过渡
使用的API:setAnimationTransition:forView:cache: 参数为:一个过渡类型,要进行过渡操作的组件的父视图,一个Boolean值用于描述是否在动画之前和之后需要Core Animation进行缓存,缓存可以提升Core Animation的性能。
4种效果:
UIViewAnimationTransitionFlipFromRight 视图内容从右向左翻转,展现屏幕背面的另外一个视图。
UIViewAnimationTransitionFlipFromLeft 视图内容从左向右翻转,展现屏幕背面的另外一个视图。
UIViewAnimationTransitionCurlUp 视图翻卷起来。
UIViewAnimationTransitionCurlDown 显示和覆盖当前视图。
看下面的例子和图示:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *animationView = [[UIView alloc] init];
animationView.backgroundColor = [UIColor redColor];
[self.view addSubview:animationView];
animationView.tag = 100;
animationView.transform = CGAffineTransformIdentity;
animationView.frame = self.view.frame;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100, 50);
[btn setTitle:@"点击看动画" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(theAnimationShow) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)theAnimationShow
{
UIView *animationView = [self.view viewWithTag:100];
[UIView beginAnimations:@"ViewAnimationOne" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(theAnimationStop:finish:context:)];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:animationView cache:YES];
[UIView commitAnimations];
}
效果图:
其它的效果只需要替换上面代码中
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:animationView cache:YES];的UIViewAnimationTransitionFlipFromRight 过渡样式即可。
按照上面介绍的顺序效果依次为:
以上图是我用模拟器截的 有的可能存在一些问题,大概是个意思….
连续变化的Core Animation层
动画有隐式和显示之分。隐式动画指的是,无须创建动画对象,只需改变动画层的属性,让核心动画自己去完成动画效果。显示动画指的是,需要自己创建和管理动画对象,并且将它们应用到动画层,才能显示动画效果。
额,关于隐式动画,由于其持续的时间比较短,几乎是看不出来的,如果要达到明显的动画效果,就需要显式的调用动画。
这里介绍下显示动画:层过渡动画
kCATransitionFade 淡入或淡出层内容,使得内容变的可见或隐藏。
kCATransitionMoveIn 要显示的层滑过底下的层
kCATransitionPush 要显示的层滑入屏幕的时候将底下的层推出屏幕
kCATransitionReveal 要显示的层渐渐显示出来,覆盖底下的层
由于要对层(layer)进行动画操作,所以需要添加QuartzCode.framework框架,并导入头文件#import
具体的操作效果看下面代码:
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UIView *layerView = [[UIView alloc] initWithFrame:self.view.frame];
[layerView setBackgroundColor:[UIColor orangeColor]];
layerView.tag = 100;
[self.view addSubview:layerView];
[layerView release];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 120, 50);
[btn setTitle:@"点击查看动画" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(theAnimtaionBegin) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)theAnimtaionBegin
{
UIView *layerView = [self.view viewWithTag:100];
CATransition *transition= [CATransition animation];
transition.duration = 1.0;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
transition.delegate = self;
transition.type = kCATransitionFade; //过渡类型,淡入或淡出
[layerView.layer addAnimation:transition forKey:@"transtionone"];
layerView.alpha = 0.0;
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
NSLog(@"....stop.....");
}
从上面的代码能正常运行,就可以发现,在Core Animation动画中,代理方法不需要设置选择器
移入、推出和显示
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UIView *layerView = [[UIView alloc] initWithFrame:self.view.frame];
[layerView setBackgroundColor:[UIColor orangeColor]];
layerView.tag = 100;
[self.view addSubview:layerView];
[layerView release];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 120, 50);
[btn setTitle:@"点击查看动画" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(theAnimtaionBegin) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)theAnimtaionBegin
{
UIView *layerView = [self.view viewWithTag:100];
CATransition *transition= [CATransition animation];
transition.duration = 1.0;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
transition.delegate = self;
transition.type = kCATransitionReveal;
transition.subtype = kCATransitionFromTop; //过渡方向
[layerView.layer addAnimation:transition forKey:@"transtionone"];
layerView.hidden = YES;
}
主要
transition.type = kCATransitionReveal;
transition.subtype = kCATransitionFromTop; //过渡方向
改变这两个的属性即可。
转载自:舵手网络--http://www.helmsmansoft.com
本文链接地址: http://www.helmsmansoft.com/index.php/archives/1499