码迷,mamicode.com
首页 > 移动开发 > 详细

IOS中的动画——Core Animation

时间:2015-04-20 16:58:14      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

一、基础动画 CABasicAnimation
1  //初始化方式
    CABasicAnimation * cabase=[CABasicAnimation animation];
2  //通过keyPath设置需要实现动画的属性,此处设为bounds
cabase.keyPath=@"bounds";
3 //通过toValue设置动画结束时候的状态
cabase.toValue=[NSValue valueWithCGRect:CGRectMake(0, 0, 10, 100)];
 //通过byValue设置每次改变的范围
cabase.byValue=[NSValue valueWithCGRect:CGRectMake(0, 0, 10, 100)];
//设置开始时候的状态
    cabase.fromValue=[NSValue valueWithCGPoint:CGPointMake(0, 0)];

4  //设置动画持续的时间
cabase.duration=2;
    //保存动画
   cabase.fillMode=kCAFillModeForwards;
    //保存设置不取消
   cabase.removedOnCompletion=NO;
[_layer addAnimation:cabase forKey:nil];

   案例:通过基础动画实现仿射变换动画

CABasicAnimation * cabase=[CABasicAnimation animation];
cabase.keyPath=@"transform";
cabase.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 2, 1)];
cabase.duration=2;
cabase.fillMode=kCAFillModeForwards;
  
    cabase.removedOnCompletion=NO;
[_layer addAnimation:cabase forKey:nil];
二、关键帧动画
1  //初始化方式
CAKeyframeAnimation * keyfram=[CAKeyframeAnimation animation];
2  //通过keyPath设置需要实现动画的属性,此处设为position
keyfram.keyPath=@"position";
3 //设置动画的需要经过的点
    CGPoint  p1=CGPointZero;
    CGPoint  p2=CGPointMake(150, 0);
    CGPoint  p3=CGPointMake(150, 150);
    CGPoint  p4=CGPointMake(0, 150);
    CGPoint  p5=CGPointZero;

    NSValue * v1=[NSValue valueWithCGPoint:p1];
    NSValue * v2=[NSValue valueWithCGPoint:p2];
    NSValue * v3=[NSValue valueWithCGPoint:p3];
    NSValue * v4=[NSValue valueWithCGPoint:p4];
NSValue * v5=[NSValue valueWithCGPoint:p5];
4 //将对应的值添加到动画并且设置动画保留
    keyfram.values=@[v1,v2,v3,v4,v5];
    keyfram.duration=1;
    keyfram.fillMode=kCAFillModeForwards;
    keyfram.removedOnCompletion=NO;
[_layer addAnimation:keyfram forKey:nil];

  案例:通过关键帧动画实现图片摇摆

  CAKeyframeAnimation * anima=[CAKeyframeAnimation animation];
    //通过设置放射变换的角度来实现
    anima.keyPath=@"transform.rotation";
    float p1=4/180.0*M_PI;
    anima.duration=0.2;
    anima.values=@[@(-p1),@(p1),@(-p1)];
    anima.fillMode=kCAFillModeForwards;
    anima.removedOnCompletion=NO;
    anima.repeatCount=MAXFLOAT;
    [_layer addAnimation:anima forKey:nil];
_layer.transform=CATransform3DMakeRotation(M_PI, 0, 0, 0);
三、转场动画
1  //初始化方式
CATransition * tran=[CATransition animation];
2  //设置动画效果
tran.type=@"rippleEffect";

//常用效果
kCATransitionFade
kCATransitionMoveIn
kCATransitionPush
kCATransitionReveal

3 //设置动画方向
tran.subtype=kCATransitionFromLeft;

//动画方向
kCATransitionFromRight
kCATransitionFromLeft
kCATransitionFromTop
kCATransitionFromBottom

4 //设置动画保留以及动画时长
  tran.fillMode=kCAFillModeForwards;
  tran.removedOnCompletion=NO;
  tran.duration=1;
  [self.myImageView.layer addAnimation:tran forKey:nil];
四、UIView封装动画

  UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画支持。执行动画所需要的工作由UIView类自动完成,但仍要在希望执行动画时通知视图,为此需要将改变属性的代码放在[UIViewbeginAnimations:nil context:nil]和[UIView commitAnimations]之间

   1、常见方法解析:

//设置动画代理
+ (void)setAnimationDelegate:(id)delegate  
 
//设置当动画即将开始时,执行delegate对象的selector,并且把beginAnimations:context:中传入的参数传进selector  
+(void)setAnimationWillStartSelector:(SEL)selector   

//设置动画结束时调用方法
+ (void)setAnimationDidStopSelector:(SEL)selector 

//设置动画持续时间
+(void)setAnimationDuration:(NSTimeInterval)duration  

//设置动画延迟
+ (void)setAnimationDelay:(NSTimeInterval)delay   

//设置动画开始时间
+ (void)setAnimationStartDate:(NSDate *)startDate

//设置动画节奏
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve 

//设置动画重复次数
+ (void)setAnimationRepeatCount:(float)repeatCount 


//如果设置为YES,代表动画每次重复执行的效果会跟上一次相反
+(void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses 

// 设置视图view的过渡效果, transition指定过渡类型, cache设置YES代表使用视图缓存,性能较好
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache 

  2、案例

   //旋转动画
    [UIView beginAnimations:@"roate" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDuration:1.5];
     [UIView setAnimationDelegate:self];
    _view.transform=CGAffineTransformRotate(_view.transform, M_PI_2);
    [UIView setAnimationDidStopSelector:@selector(endAnimate)];
[UIView commitAnimations];
    //转场动画
[UIView beginAnimations:@"transition" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_mainView cache:YES];
    [UIView setAnimationDuration:1.5];
    NSInteger index1=[_mainView.subviews indexOfObject:_view];
    NSInteger index2=[_mainView.subviews indexOfObject:_view2];
    [_mainView exchangeSubviewAtIndex:index1 withSubviewAtIndex:index2];
    [UIView commitAnimations];

IOS中的动画——Core Animation

标签:

原文地址:http://blog.csdn.net/qq_24908939/article/details/45151255

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!