#import "ViewController.h"
@interface ViewController ()
{
CALayer *layer;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *bgimageView = [[UIImageView alloc]initWithFrame:self.view.frame];
bgimageView.image = [UIImage imageNamed:@"1.jpg"];
[self.view addSubview:bgimageView];
[self addFollower];
[self addAnimationGroup];
}
- (void)addFollower{
UIImage *image = [UIImage imageNamed:@"2"];
layer = [[CALayer alloc]init];
layer.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
layer.position = CGPointMake(100, 200);
layer.contents = (id)(image.CGImage);
[self.view.layer addSublayer:layer];
}
#pragma mark 添加动画组
- (void)addAnimationGroup{
CAAnimationGroup *Group = [[CAAnimationGroup alloc]init];
Group.animations = @[[self rotationAnimation],[self dropAnimation]];
Group.duration = 10;
Group.beginTime = CACurrentMediaTime()+5;
Group.removedOnCompletion = NO;
Group.fillMode = kCAFillModeBoth;
[layer addAnimation:Group forKey:@"Group"];
}
#pragma mark 花瓣旋转
- (CABasicAnimation *)rotationAnimation{
CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotation.toValue = @(M_PI_2*3);
rotation.removedOnCompletion = NO;
return rotation;
}
#pragma mark 掉落的路径
- (CAKeyframeAnimation *)dropAnimation{
CAKeyframeAnimation *drop = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGMutablePathRef ref = CGPathCreateMutable();
CGPathMoveToPoint(ref, NULL, layer.position.x, layer.position.y);
CGPoint endPoint = CGPointMake(80, 580);
CGPathAddCurveToPoint(ref, NULL, 160, 280, -30, 300, endPoint.x, endPoint.y);
drop.path = ref;
CGPathRelease(ref);
return drop;
}