这两种动画效果是一样的,都是淡出效果,两种代码形式不同。对比看一下吧。
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY([UIScreen mainScreen].bounds)-44-20, 320, 44)];
(1) CABasicAnimation *positionAnim=[CABasicAnimation animationWithKeyPath:@"position"];
[positionAnim setFromValue:[NSValue valueWithCGPoint:CGPointMake(view.center.x,view.center.y)]];
[positionAnim setToValue:[NSValue valueWithCGPoint:CGPointMake(view.center.x,view.center.y-_keyboardRect.size.height)]];
[positionAnim setDelegate:self];
[positionAnim setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[positionAnim setDuration:0.25];
[view.layer addAnimation:positionAnim forKey:nil];
[view setCenter:CGPointMake(view.center.x,view.center.y-_keyboardRect.size.height)];//如果不加次行代码,动画完会恢复原位
(2)[UIView animateWithDuration:0.25
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
view.transform= CGAffineTransformMakeTranslation(0, -_keyboardRect.size.height-20+[self getHeighOfWindow]-CGRectGetMaxY(self.originalFrame));
} completion:nil];
用第二种方法里的CGAffineTransformMakeTranslation(x,y)时要注意了。这时以view的坐标为(0,0),上移Y加上负值,下移Y加上正直。左移X加正直,有移X轴加负值。
用第一种动画时,坐标系就好比下图所示。貌似跟第四象限差不多,不同的是这个的Y轴也是正的。向上移动就是原来的坐标减去要移动的大小。
(3)CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = kDuration;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.type = kCATransitionPush;
animation.subtype = kCATransitionFromTop;
[self setAlpha:1.0f];
[self.layer addAnimation:animation forKey:@"DDLocateView"];
需要在frameworks中添加QuartzCore.framework
在接口程序中加上头文件 #import <QuartzCore/QuartzCore.h>
/*好多动画效果*/
CATransition *transition = [CATransition animation];
transition.duration = 1.0f; /* 间隔时间*/
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; /* 动画的开始与结束的快慢*/
transition.type = @"rippleEffect"; /* 各种动画效果*/
//@"cube" @"moveIn" @"reveal" @"fade"(default) @"pageCurl" @"pageUnCurl" @"suckEffect" @"rippleEffect" @"oglFlip"
transition.subtype = kCATransitionFromRight; /* 动画方向*/
transition.delegate = self;
[navigationController.view.layer /* 在想添加CA动画的VIEW的层上添加此代码*/addAnimation:transition forKey:nil];
//另外加一句,transition在申请时用的是+方法,所以不需要自己进行release ,在层上添加后不要认为retainCount已经+1,就还要release
//实际上CATransition类中还有一个属性是removedOnCompletion,是此动画执行完后会自动remove,默认值为true
CATransition的type属性
这里使用了setType与setSubtype组合,这使用个比较保险,因为他的参数就是官方API里定义的,他们的参数说明可以参考如下:
[c] view plaincopy
<strong>setType<span style="color:#339933">:</span>可以返回四种类型:</strong>
kCATransitionFade淡出
kCATransitionMoveIn覆盖原图
kCATransitionPush推出
kCATransitionReveal底部显出来
<strong>setSubtype<span style="color:#339933">:</span>也可以有四种类型:</strong>
kCATransitionFromRight;
kCATransitionFromLeft<span style="color:#009900">(</span>默认值<span style="color:#009900">)</span>
kCATransitionFromTop;
kCATransitionFromBottom
还有一种设置动画类型的方法,不用setSubtype,只用setType
[c] view plaincopy
animation.type=@"pageCurl";
[c] view plaincopy
pageCurl 向上翻一页
pageUnCurl 向下翻一页
rippleEffect 滴水效果
suckEffect 收缩效果,如一块布被抽走
cube 立方体效果
oglFlip 上下翻转效果