CAAnimationGroup
/*
CAAnimationGroup
1、动画的数组 animations
2、启动的时间 beginTime
动画组设置了持续时间 可能就会导致动画组里面的某一个动画的 持续时间 没响应(失效)
*/
#import "ViewController.h"
@interface ViewController ()
{
CALayer *petal;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addBgView];
[self addPetaller];
[self addAnimationGroup];
}
- (CAKeyframeAnimation *)dropAnimation
{
CAKeyframeAnimation *drop = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGMutablePathRef pathRef = CGPathCreateMutable();//创建路径
CGPathMoveToPoint(pathRef, NULL, petal.position.x, petal.position.y);
//CGPathAddCurveToPoint cp1x y cpx y 设置两个点 在这两个点之间取画曲线
//<#CGFloat x#> <#CGFloat y#> 终止点
CGPoint endPoint = CGPointMake(80, 500);
CGPathAddCurveToPoint(pathRef, NULL, 160, 280,-30 , 300, endPoint.x, endPoint.y);
drop.path = pathRef;
CGPathRelease(pathRef);
return drop;
}
- (CABasicAnimation *)rotationAnimation
{
CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotation.toValue = @(M_PI_2*3);
rotation.removedOnCompletion = YES;
return rotation;
}
#pragma mark -----添加动画数组-----
- (void)addAnimationGroup
{
//初始化分组动画
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[[self rotationAnimation],[self dropAnimation]];
group.duration = 5;
//beginTime 动画开始的时间(10秒之后,延迟加载)
//CACurrentMediaTime 获得当前的时间
//从调用这个方法开始 10秒 之后 执行动画
group.beginTime = CACurrentMediaTime() + 1;
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeBoth;
[petal addAnimation:group forKey:@"group"];
}
- (void)addPetaller
{
UIImage *image = [UIImage imageNamed:@"petal.jpg"];
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];
}
- (void)addBgView
{
UIImageView *bgImgView = [[UIImageView alloc]initWithFrame:self.view.frame];
bgImgView.image = [UIImage imageNamed:@"C7QB825$S`(D65_TTRBBX@1.jpg"];
[self.view addSubview:bgImgView];
}