#import "ViewController.h"
@interface ViewController ()
{
CALayer *layer;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/*
CAAnimationGroup的属性是:
1、动画的数组 animations
2、启动时间:beginTime
动画组设置持续时间(duration) 可能会导致动画组里面的动画持续时间失效
*/
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]];
// 添加到layer中
Group.duration = 10;
// beginTime动画开始的时间
// 从调用这个方法开始 10秒后执行动画
Group.beginTime = CACurrentMediaTime()+5;//CACurrentMediaTime获得当前时间,来自于继承中的属性
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);
// 设置两个点,在这两个点之间去画曲线cp1和cp2 x,y是终止点
CGPoint endPoint = CGPointMake(80, 580);
CGPathAddCurveToPoint(ref, NULL, 160, 280, -30, 300, endPoint.x, endPoint.y);
drop.path = ref;
CGPathRelease(ref);//释放
return drop;
}
动画特效之动画组
最新推荐文章于 2023-04-05 00:36:39 发布