我的博客大部分都是从笔记直接拉过来的,没有精雕细琢,请见谅。这一篇最后一段代码还是有点用的。
1. 基本动画
//动画
[UIView beginAnimations:nil context:nil];
//设定动画持续时间
[UIView setAnimationDuration:2];
//动画的内容
frame.origin.y += 150;
[imageView setFrame:frame];
//动画结束
[UIView commitAnimations];
2.动画切换页面
地址:http://blog.163.com/wzi_xiang/blog/static/65982961201211193275578/
由VIewController跳转到VIewController
代码:
//自定义动画
CATransition *transition = [CATransition animation];
transition.duration = 2.0f;
//动画时间控制
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//是否代理
transition.delegate = self;
//是否在当前层完成动画
transition.removedOnCompletion = NO;
//设置动画效果:这两个参数是关键
transition.type = @"cameraIrisHollowOpen";
transition.subtype = kCATransitionMoveIn;
AnimationViewController *animationController = [[AnimationViewController alloc]init];
//注意此处设置为NO
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationDelegate:self];//是否需要实现协议
// [UIView setAnimationDidStopSelector:@selector(pushAnimationDidStop)];
// [UIView setAnimationTransition:transition forView:self.view cache:YES];
// [UIView commitAnimations];
[self.navigationController pushViewController:animationController animated:NO];
[self.navigationController.view.layer addAnimation:transition forKey:nil];
3.添加View时的动画(参见程序AnimationTest2)
//关键语句
[view.layer addAnimation:animation forKey:@"animation”];
4.Transition关键词
对应的type有一下类型(共计16种):
animation.type =
kCATransitionFade;
kCATransitionPush;
kCATransitionReveal;
kCATransitionMoveIn;
@“cube";
@“suckEffect";
@“oglFlip";
@“rippleEffect";
@“pageCurl";
@“pageUnCurl";
@“cameraIrisHollowOpen";
@“cameraIrisHollowClose";
//这四种使用方法和以上12种不一样(见第5点)
UIViewAnimationTransitionCurlDown;
UIViewAnimationTransitionCurlUp;
UIViewAnimationTransitionFlipFromLeft;
UIViewAnimationTransitionFlipFromRight;
对应的subtype有四种:(对应4个方向)
kCATransitionFromLeft;
kCATransitionFromBottom;
kCATransitionFromRight;
kCATransitionFromTop;
5. #pragma UIView实现动画
- (void)animationWithView:(UIView *)view WithAnimationTransition:(UIViewAnimationTransition)transition
{
[UIView animateWithDuration:2.0f animations:^{
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:transition forView:view cache:YES];
}];
}
6.可调用动画方法
#pragma CATransiton动画实现
- (void)transitionWithType:(NSString *)type WithSubtype:(NSString *)subtype
ForView:(UIView *)view
{
//创建CATransition对象
CATransition *animation = [CATransition animation];
animation.duration = 2.0;
animation.subtype =
//设置运动type
animation.type = type;
if (subtype != nil)
{
//设置子类
animation.subtype = subtype;
}
//设置运动速度
animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
[view.layer addAnimation:animation forKey:@"animation"];
}
7.从ViewController跳转到另一个
所有视图控制器继承BaseController:
//BaseController.h
typedef enum
{
Transition_left = 5678,//从左侧滑出,返回
Transition_right,//从右侧滑出,push
Transition_reveal,//揭开效果
Transition_moveIn,//覆盖效果
Transition_cube,//立方体效果
Transition_suckEffect,//吮吸效果
Transition_oglFlip,//翻转效果
Transition_RippleEffect,//波纹
Transition_pageCurl,//翻页
Transition_pageUnCurl,//反翻页
Transition_cameraOpen,//开镜头
Transition_cameraClose,//关镜头
Transition_curlDown,//下翻页效果
Transition_curlUp,//上翻页效果
Transition_flipLeft,//左翻转效果
Transition_flipRight,//右翻转效果
}TransitionEffect;
@interface BaseController : UIViewController
/**
* 跳转到某个继承本类的ViewController:如果该类已经存在,则pop回去;如果不存在则push
*
* @param vc 要跳转的页面
* @param effect 跳转效果:可以指定向左跳转,向右跳转及其他效果
*/
- (void)pushToVc:(UIViewController *)vc withTransition:(TransitionEffect)effect;
@end
//BaseController.h
- (void)pushToVc:(BaseController *)vc withTransition:(TransitionEffect)effect
{
for (UIViewController *viewController in self.navigationController.viewControllers)
{
if ([viewController isKindOfClass:[vc class]])
{
[self gotoViewController:viewController withTransition:effect isPush:NO];
return;
}
}
[self gotoViewController:vc withTransition:effect isPush:YES];
}
- (void)gotoViewController:(UIViewController *)vc withTransition:(TransitionEffect)effect isPush:(BOOL)ispush
{
switch (effect)
{
case Transition_left:
[self transitionWithType:kCATransitionPush WithSubtype:kCATransitionFromLeft ForView:self.navigationController.view];
break;
case Transition_right:
[self transitionWithType:kCATransitionPush WithSubtype:kCATransitionFromRight ForView:self.navigationController.view];
break;
case Transition_reveal:
[self transitionWithType:kCATransitionReveal WithSubtype:kCATransitionFromLeft ForView:self.navigationController.view];
break;
case Transition_moveIn:
[self transitionWithType:kCATransitionMoveIn WithSubtype:kCATransitionFromLeft ForView:self.navigationController.view];
break;
case Transition_cube:
[self transitionWithType:@"cube" WithSubtype:kCATransitionFromLeft ForView:self.navigationController.view];
break;
case Transition_suckEffect:
[self transitionWithType:@"suckEffect" WithSubtype:kCATransitionFromLeft ForView:self.navigationController.view];
break;
case Transition_oglFlip:
[self transitionWithType:@"oglFlip" WithSubtype:kCATransitionFromLeft ForView:self.navigationController.view];
break;
case Transition_RippleEffect:
[self transitionWithType:@"rippleEffect" WithSubtype:kCATransitionFromLeft ForView:self.navigationController.view];
break;
case Transition_pageCurl:
[self transitionWithType:@"pageCurl" WithSubtype:kCATransitionFromLeft ForView:self.navigationController.view];
break;
case Transition_pageUnCurl:
[self transitionWithType:@"pageUnCurl" WithSubtype:kCATransitionFromLeft ForView:self.navigationController.view];
break;
case Transition_cameraOpen:
[self transitionWithType:@"cameraIrisHollowOpen" WithSubtype:kCATransitionFromLeft ForView:self.navigationController.view];
break;
case Transition_cameraClose:
[self transitionWithType:@"cameraIrisHollowClose" WithSubtype:kCATransitionFromLeft ForView:self.navigationController.view];
break;
case Transition_curlDown:
[self animationWithView:self.navigationController.view WithAnimationTransition:UIViewAnimationTransitionCurlDown];
break;
case Transition_curlUp:
[self animationWithView:self.navigationController.view WithAnimationTransition:UIViewAnimationTransitionCurlUp];
break;
case Transition_flipLeft:
[self animationWithView:self.navigationController.view WithAnimationTransition:UIViewAnimationTransitionFlipFromLeft];
break;
case Transition_flipRight:
[self animationWithView:self.navigationController.view WithAnimationTransition:UIViewAnimationTransitionFlipFromRight];
break;
default:
break;
}
if (ispush)
{
[self.navigationController pushViewController:vc animated:NO];
}
else
{
[self.navigationController popToViewController:vc animated:NO];
}
}
#pragma CATransiton动画实现
- (void)transitionWithType:(NSString *)type WithSubtype:(NSString *)subtype
ForView:(UIView *)view
{
//创建CATransition对象
CATransition *animation = [CATransition animation];
animation.duration = 0.5;
//设置运动type
animation.type = type;
if (subtype != nil)
{
//设置子类
animation.subtype = subtype;
}
//设置运动速度
animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
[view.layer addAnimation:animation forKey:@"animation"];
}
#pragma UIView实现动画
- (void)animationWithView:(UIView *)view WithAnimationTransition:(UIViewAnimationTransition)transition
{
[UIView animateWithDuration:0.5 animations:^{
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:transition forView:view cache:YES];
}];
}