首页 > 代码库 > CoreAnimation 核心动画

CoreAnimation 核心动画

- (void)createBaseAnimation{

    //基础动画

    CABasicAnimation *animation = [CABasicAnimation animation];

    

    animation.keyPath = @"bounds";

//    animation.fromValue = http://www.mamicode.com/[NSValue valueWithCGRect:CGRectMake(0, 0, 300, 300)];//默认为现在的状态

    animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 300, 300)];

    

    animation.duration = 2;

    animation.removedOnCompletion = NO//动画之后保持最新

    animation.fillMode = kCAFillModeForwards//最新样式

    

    animation.delegate = self;

    [view.layer addAnimation:animation forKey:nil];

 

}

 

- (void)animationDidStart:(CAAnimation *)anim{

    NSLog(@"动画开始时调用");

    

}

 

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{

    NSLog(@"动画结束时调用");

}

 

- (void)createKeyFrameAnimation{

    //帧动画

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];

    animation.keyPath = @"position";

    //路径

//    CGMutablePathRef path = CGPathCreateMutable();

//    CGPathAddEllipseInRect(path, nil, CGRectMake(100, 100, 200, 200));

//    

//    animation.path = path;

    

//注意:与基础动画的区别:可以设置多个点/一堆值

    NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(0, 0)];

    NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(0, 300)];

    NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(300, 0)];

    animation.values = @[value1,value2,value3];

    //动画的执行节奏

    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

    animation.duration = 5;

    animation.removedOnCompletion = NO//动画之后保持最新

    animation.fillMode = kCAFillModeForwards//最新样式

    [view.layer addAnimation:animation forKey:nil];

    

}

 

- (void)createTransiton{

    //转场动画

    CATransition *animaiton = [CATransition animation];

    //动画过渡类型

    animaiton.type = @"cube";

    //过渡方向

    animaiton.subtype = kCATransitionFade;

    

    animaiton.duration = 3;

    [view.layer addAnimation:animaiton forKey:nil];

 

}

 

- (void)createAnimationGroup{

    //动画分组(旋转+缩放)

    //1.旋转

    CABasicAnimation *rotationAnimaiton = [CABasicAnimation animation];

    rotationAnimaiton.keyPath = @"transform.rotation.z";

    rotationAnimaiton.toValue = @(M_PI_4 * 3);

    rotationAnimaiton.duration = 5;

    rotationAnimaiton.repeatCount = MAXFLOAT;

    

    //2.缩放

    CABasicAnimation *scaleAnimation = [CABasicAnimation animation];

    scaleAnimation.keyPath = @"transform";

    scaleAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2, 2, 0)];

    scaleAnimation.duration = 5;

    

    //动画分组

    CAAnimationGroup *group = [CAAnimationGroup animation];

    group.animations = @[rotationAnimaiton,scaleAnimation];

    group.removedOnCompletion = NO;

    group.fillMode = kCAFillModeForwards;

    group.duration = 5;

    [view.layer addAnimation:group forKey:nil];

    

}

 

 

 

 

 

CoreAnimation 核心动画