CABasicAnimation汇总

来源:互联网 发布:声音波形分析软件 编辑:程序博客网 时间:2024/05/29 15:04

CABasicAnimation animationWithKeyPath 一些规定的值

http://www.cnblogs.com/pengyingh/articles/2379631.html

CABasicAnimation *theAnimation;theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];theAnimation.delegate = self;theAnimation.duration = 1;theAnimation.repeatCount = 0;theAnimation.removedOnCompletion = FALSE;theAnimation.fillMode = kCAFillModeForwards;theAnimation.autoreverses = NO;theAnimation.fromValue = [NSNumber numberWithFloat:0];theAnimation.toValue = [NSNumber numberWithFloat:-60]; [self.view.layer addAnimation:theAnimation forKey:@"animateLayer"];
我们可以通过animationWithKeyPath键值对的方式来改变动画animationWithKeyPath的值: transform.scale = 比例轉換transform.scale.x = 闊的比例轉換transform.scale.y = 高的比例轉換transform.rotation.z = 平面圖的旋轉opacity = 透明度 marginzPosition backgroundColor cornerRadiusborderWidth boundscontentscontentsRectcornerRadiusframehiddenmaskmasksToBoundsopacitypositionshadowColorshadowOffsetshadowOpacityshadowRadius[self. ui_View.layer removeAllAnimations];        CABasicAnimation *pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];    pulse.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];    pulse.duration = 0.5 + (rand() % 10) * 0.05;    pulse.repeatCount = 1;    pulse.autoreverses = YES;    pulse.fromValue = [NSNumber numberWithFloat:.8];    pulse.toValue = [NSNumber numberWithFloat:1.2];    [self.ui_View.layer addAnimation:pulse forKey:nil];// bounds CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"bounds"];    anim.duration = 1.f;    anim.fromValue = [NSValue valueWithCGRect:CGRectMake(0,0,10,10)];    anim.toValue = [NSValue valueWithCGRect:CGRectMake(10,10,200,200)];    anim.byValue  = [NSValue valueWithCGRect:self. ui_View.bounds]; //    anim.toValue = (id)[UIColor redColor].CGColor;//    anim.fromValue =  (id)[UIColor blackColor].CGColor;        anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];    anim.repeatCount = 1;    anim.autoreverses = YES;        [ui_View.layer addAnimation:anim forKey:nil];//cornerRadius     CABasicAnimation *anim2 = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];    anim2.duration = 1.f;    anim2.fromValue = [NSNumber numberWithFloat:0.f];    anim2.toValue = [NSNumber numberWithFloat:20.f];    anim2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];    anim2.repeatCount = CGFLOAT_MAX;    anim2.autoreverses = YES;        [ui_View.layer addAnimation:anim2 forKey:@"cornerRadius"];//contents CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"contents"];    anim.duration = 1.f;    anim.fromValue = (id)[UIImage imageNamed:@"1.jpg"].CGImage;    anim.toValue = (id)[UIImage imageNamed:@"2.png"].CGImage;//    anim.byValue  = (id)[UIImage imageNamed:@"3.png"].CGImage;//    anim.toValue = (id)[UIColor redColor].CGColor;//    anim.fromValue =  (id)[UIColor blackColor].CGColor;        anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];    anim.repeatCount = CGFLOAT_MAX;    anim.autoreverses = YES;        [ui_View.layer addAnimation:anim forKey:nil]; [ui_View.layer setShadowOffset:CGSizeMake(2,2)];    [ui_View.layer setShadowOpacity:1];    [ui_View.layer setShadowColor:[UIColor grayColor].CGColor];//    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"shadowColor"];    anim.duration = 1.f;    anim.toValue = (id)[UIColor redColor].CGColor;    anim.fromValue =  (id)[UIColor blackColor].CGColor;        anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];    anim.repeatCount = CGFLOAT_MAX;    anim.autoreverses = YES;        [ui_View.layer addAnimation:anim forKey:nil];        CABasicAnimation *_anim = [CABasicAnimation animationWithKeyPath:@"shadowOffset"];    _anim.duration = 1.f;    _anim.fromValue = [NSValue valueWithCGSize:CGSizeMake(0,0)];    _anim.toValue = [NSValue valueWithCGSize:CGSizeMake(3,3)];        _anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];    _anim.repeatCount = CGFLOAT_MAX;    _anim.autoreverses = YES;        [ui_View.layer addAnimation:_anim forKey:nil];            CABasicAnimation *_anim1 = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];    _anim1.duration = 1.f;    _anim1.fromValue = [NSNumber numberWithFloat:0.5];    _anim1.toValue = [NSNumber numberWithFloat:1];        _anim1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];    _anim1.repeatCount = CGFLOAT_MAX;    _anim1.autoreverses = YES;        [ui_View.layer addAnimation:_anim1 forKey:nil];                CABasicAnimation *_anim2 = [CABasicAnimation animationWithKeyPath:@"shadowRadius"];    _anim2.duration = 1.f;    _anim2.fromValue = [NSNumber numberWithFloat:10];    _anim2.toValue = [NSNumber numberWithFloat:5];        _anim2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];    _anim2.repeatCount = CGFLOAT_MAX;    _anim2.autoreverses = YES;        [ui_View.layer addAnimation:_anim2 forKey:nil];



- (void)viewDidLoad {    [super viewDidLoad];layer=[CALayer layer];layer.frame=CGRectMake(50, 200, 50, 50);layer.backgroundColor=[UIColor orangeColor].CGColor;layer.cornerRadius=8.0f;CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];animation.duration=4.0f;animation.autoreverses=NO;animation.repeatCount=1;animation.toValue=[NSNumber numberWithInt:-10];animation.fromValue=[NSNumber numberWithInt:200];animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];CABasicAnimation *animationZoomIn=[CABasicAnimation animationWithKeyPath:@"transform.scale"];animationZoomIn.duration=2.0f;animationZoomIn.autoreverses=NO;animationZoomIn.repeatCount=1;animationZoomIn.toValue=[NSNumber numberWithFloat:1.56];animationZoomIn.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];CABasicAnimation *animationZoomOut=[CABasicAnimation animationWithKeyPath:@"transform.scale"];animationZoomOut.beginTime=2.0f;animationZoomOut.duration=2.0f;animationZoomOut.autoreverses=NO;animationZoomOut.repeatCount=1;animationZoomOut.toValue=[NSNumber numberWithFloat:.01];animationZoomOut.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];CAAnimationGroup *group=[CAAnimationGroup animation];group.duration=4.0f;group.animations=[NSArray arrayWithObjects: animation, animationZoomIn, animationZoomOut,nil];group.removedOnCompletion=NO;group.fillMode=kCAFillModeForwards;[layer addAnimation:group forKey:nil];[self.view.layer addSublayer:layer];//layer.hidden=YES;}




基于CABasicAnimation、CAAnimationGroup组合动画示例

http://www.cocoachina.com/bbs/simple/?t27152.html

在做动画时,我们希望将几种动画叠加起来同时播放,或者依次连续播放。
我们可以通过CABasicAnimation和CAAnimationGroup来实现。
下面我把实现的主要函数贴出来,程序工程也一并发布(更新:直接给视图的层添加所需的动画,不需要另创建一个层对象):

#define ANIM_ROTATE        @"animationRotate"#define ANIM_FALLING    @"animationFalling"#define ANIM_GROUP        @"animationFallingRotate"- (void)viewDidLoad {    [super viewDidLoad];        UIImage* image = [UIImage imageNamed:@"image.png"];    m_pMyImageView = [[UIImageView alloc] initWithImage:image];        m_pMyImageView.center = CGPointMake(384, 50);    [self.view addSubview:m_pMyImageView];        //在层上做旋转动画    CAAnimation* myAnimationRotate    = [self animationRotate];;    CAAnimation* myAnimationFallingDown        = [self animationFallingDown];;    CAAnimation* myAnimationShrink            = [self animationShrink];    #if 0//method1:依次把各个动画加入层中    [m_pMyImageView.layer addAnimation:myAnimationRotateForever forKey:ANIM_ROTATE];    [m_pMyImageView.layer addAnimation:myAnimationFallingDown forKey:ANIM_FALLING];    #else//work well :)    //method2:放入动画数组,统一处理!    m_pGroupAnimation    = [CAAnimationGroup animation];        //设置动画代理    m_pGroupAnimation.delegate = self;        m_pGroupAnimation.removedOnCompletion = NO;        m_pGroupAnimation.duration             = 2.0;    m_pGroupAnimation.timingFunction      = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];        m_pGroupAnimation.repeatCount         = 1;//FLT_MAX;  //"forever";    m_pGroupAnimation.fillMode             = kCAFillModeForwards;    m_pGroupAnimation.animations             = [NSArray arrayWithObjects:myAnimationRotate,                                                                          myAnimationFallingDown,                                                                          myAnimationShrink,                                                                         nil];    //对视图自身的层添加组动画    [m_pMyImageView.layer addAnimation:m_pGroupAnimation forKey:ANIM_GROUP];    #endif    }


/* * 1、make rotate */- (CAAnimation *)animationRotate{    // rotate animation    CATransform3D rotationTransform  = CATransform3DMakeRotation(M_PI, 1.0, 0, 0.0);        CABasicAnimation* animation;    animation = [CABasicAnimation animationWithKeyPath:@"transform"];        animation.toValue        = [NSValue valueWithCATransform3D:rotationTransform];    animation.duration        = 0.5;    animation.autoreverses    = NO;    animation.cumulative    = YES;    animation.repeatCount    = FLT_MAX;  //"forever"    //设置开始时间,能够连续播放多组动画    animation.beginTime        = 0.5;    //设置动画代理    animation.delegate        = self;    return animation;}/* * 2、fall down */- (CAAnimation *)animationFallingDown{    //falling down animation:    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];        animation.duration                = 2.0;    animation.autoreverses            = NO;    animation.removedOnCompletion    = NO;    animation.repeatCount            = FLT_MAX;  //"forever"    animation.fromValue                = [NSNumber numberWithInt: 0];    animation.toValue                = [NSNumber numberWithInt: 1024];    //设置动画代理    animation.delegate                = self;    return animation;}/* * 3、shrink animation */- (CAAnimation *)animationShrink{       CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];        animation.toValue = [NSNumber numberWithDouble:2.0];        animation.duration                = 2.0;    animation.autoreverses            = YES;    animation.repeatCount            = FLT_MAX;  //"forever"    animation.removedOnCompletion    = NO;    //设置动画代理    animation.delegate                = self;    return animation;}


/* *动画结束后的委托函数,移除动画视图 */- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{    //这边为什么是nil? :(    NSLog(@"anim = %@", [m_pMyImageView.layer valueForKey:ANIM_GROUP]);#if 1    //识别动画    //?debug发现两者的地址不一样,这个问题很纠结    if ([anim isEqual:m_pGroupAnimation])//[m_pMyImageView.layer valueForKey:ANIM_GROUP])    {        NSLog(@"removeFromSuperview...");        [m_pMyImageView removeFromSuperview];        [m_pMyImageView release];    }#else    //这种方法,虽然能解决方法,但是处理多个CAAnimationGroup动画或者CAAnimation动画时,就不能有效处理,方法待定    //这组动画结束,移除视图    if ([anim isKindOfClass:[CAAnimationGroup class]])    {        //这边为什么是nil? :(        NSLog(@"anim = %@", [m_pMyImageView.layer valueForKey:ANIM_GROUP]);                [m_pMyImageView removeFromSuperview];        [m_pMyImageView release];    }#endif    }


update at 2010-10-25:
只是把初始化修改一下,代码思路更直观点,但是关于在动画结束代理中,识别层指定的动画方法仍不起作用,看了有关帖子,不过仍无果,请教大家?


补充一个缩放的动画函数,上面的demo我也重新更新一下!

/* *shrink animation */- (CAAnimation *)animationShrink{       CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];        animation.toValue = [NSNumber numberWithDouble:2.0];        animation.duration                = 2.0;    animation.autoreverses            = YES;    animation.repeatCount            = FLT_MAX;  //"forever"    animation.removedOnCompletion    = NO;    //设置动画代理    animation.delegate                = self;    return animation;}


原创粉丝点击