iOS动画组基础

CAAnimationGroup最核心的属性有animations

以下是关于核心动画的代码:

/**

 CAAnimationGroup

 1.动画的数组  animations

 2.beginTime

 动画组  设置了持续事件   可能会导致动画组里面的动画的持续时间无效

 

 */

#import "ViewController.h"


@interface ViewController ()

{

    CALayer *petal;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    [self addBgView];

    [self addPetalLayer];

    [self addAnimationGroup];

}

- (void)addBgView

{

    UIImageView *bgView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];

    bgView.image = [UIImage imageNamed:@"背景.jpg"];

    [self.view addSubview:bgView];

}

- (void)addPetalLayer

{

    UIImage *image = [UIImage imageNamed:@"花瓣"];

    petal = [[CALayer alloc]init];

    petal.position = CGPointMake(100, 200);

    petal.bounds = CGRectMake(0, 0, image.size.width, image.size.height);

    petal.contents = (id)image.CGImage;

    [self.view.layer addSublayer:petal];

}

- (CABasicAnimation *)rotationAnimation

{

    CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

    rotation.toValue = @(M_2_PI*3);

    rotation.removedOnCompletion = NO;

    

    return rotation;

}

- (CAKeyframeAnimation *)dropAnimation

{

    CAKeyframeAnimation *drop = [CAKeyframeAnimation animationWithKeyPath:@"position"];

    CGMutablePathRef pathRef = CGPathCreateMutable();

    CGPathMoveToPoint(pathRef, NULL, petal.position.x, petal.position.y);

    CGPoint endPoint  = CGPointMake(80, 560);

//    CGPathAddCurveToPoint cp1x y cpx y 设置两个点 在这两个点之间画曲线

//    x y 终止点

    CGPathAddCurveToPoint(pathRef, NULL, 160, 280, -30, 300, endPoint.x, endPoint.y);


    drop.path = pathRef;

    CGPathRelease(pathRef);

    return drop;

}


#pragma mark ---添加动画组----

- (void)addAnimationGroup

{

    CAAnimationGroup *group = [CAAnimationGroup animation];

    group.animations = @[[self rotationAnimation],[self dropAnimation]];

    group.duration = 5;

//    CACurrentMediaTime是获得当前的时间

    group.beginTime = CACurrentMediaTime()+5;

    group.removedOnCompletion = NO;

    group.fillMode = kCAFillModeBoth;

    [petal addAnimation:group forKey:@"group"];

    

}

@end














你可能感兴趣的:(iOS动画组基础)