Ios 如何在CAKeyFrameAnimation关键帧的值更改时为CAShapeLayer绘制新的UIBezierPath
各位 我成功地使用Ios 如何在CAKeyFrameAnimation关键帧的值更改时为CAShapeLayer绘制新的UIBezierPath,ios,Ios,各位 我成功地使用 [CAKeyframeAnimation animationWithKeyPath:@"bounds.size"] (它使CAShapeLayer逐渐变大) 现在,我需要在CAShapeLayer中根据bounds.size的当前大小,随着动画的进行,每次都画一个圆圈 如何做到这一点? 谢谢 这是一个工作代码(为了简单起见,我用一个移动条更改了圆) 当anim1和anim2运行时,我们希望确保anim2中的“高度”与anim1中不断变化的“高度”值同步 CAShapeLay
[CAKeyframeAnimation animationWithKeyPath:@"bounds.size"]
(它使CAShapeLayer
逐渐变大)
现在,我需要在CAShapeLayer
中根据bounds.size
的当前大小,随着动画的进行,每次都画一个圆圈
如何做到这一点?
谢谢
这是一个工作代码(为了简单起见,我用一个移动条更改了圆)
当anim1和anim2运行时,我们希望确保anim2中的“高度”与anim1中不断变化的“高度”值同步
CAShapeLayer * myShapeLayer = [[CAShapeLayer alloc] init];
myShapeLayer.anchorPoint = CGPointMake(0, 0);
myShapeLayer.bounds = CGRectMake(0, 0, 60, 60);
myShapeLayer.position = CGPointMake(35, 30);
[myShapeLayer setBorderWidth:1.0];
[myShapeLayer setFillColor:[UIColor clearColor].CGColor];
myShapeLayer.strokeColor = [[UIColor blueColor] CGColor];
myShapeLayer.lineWidth = 2;
[self.view.layer addSublayer:myShapeLayer];
NSMutableArray * boxsizes = [[NSMutableArray alloc] initWithObjects:
[NSValue valueWithCGSize:CGSizeMake(50, 50)],
[NSValue valueWithCGSize:CGSizeMake(50, 60)],
[NSValue valueWithCGSize:CGSizeMake(50, 70)],
nil];
CAKeyframeAnimation * anim1;
anim1=[CAKeyframeAnimation animationWithKeyPath:@"bounds.size"];
anim1.values = boxsizes;
anim1.duration=10.0;
CGMutablePathRef pA = CGPathCreateMutable();
CGPathMoveToPoint (pA,NULL,myShapeLayer.bounds.origin.x, 10);
CGPathAddLineToPoint(pA,NULL,myShapeLayer.bounds.origin.x+10,10);
CGMutablePathRef pB = CGPathCreateMutable();
CGPathMoveToPoint (pB,NULL,myShapeLayer.bounds.origin.x, myShapeLayer.bounds.size.height);
CGPathAddLineToPoint(pB,NULL,myShapeLayer.bounds.origin.x+10,myShapeLayer.bounds.size.height);
NSMutableArray * pathX = [[NSMutableArray alloc] initWithObjects:
(__bridge id) pA,
(__bridge id) pB,
nil];
CAKeyframeAnimation * anim2;
anim2=[CAKeyframeAnimation animationWithKeyPath:@"path"];
anim2.values = pathX;
anim2.duration=10.0;
CAAnimationGroup* group1 = [CAAnimationGroup animation];
group1.animations = [NSArray arrayWithObjects:anim1,anim2, nil];
group1.duration = 10.0;
[myShapeLayer addAnimation:group1 forKey:@"Somekeyhere"];
在路径上执行并行动画,并为每个边界关键帧设置新路径 要确保椭圆的大小与第一个动画的大小对齐,请从第一个动画使用的大小导出路径。以下是您的代码的修改版本:
CAShapeLayer * myShapeLayer = [[CAShapeLayer alloc] init];
myShapeLayer.anchorPoint = CGPointMake(0, 0);
myShapeLayer.bounds = CGRectMake(0, 0, 60, 60);
myShapeLayer.position = CGPointMake(35, 30);
[myShapeLayer setBorderWidth:1.0];
[myShapeLayer setFillColor:[UIColor clearColor].CGColor];
myShapeLayer.strokeColor = [[UIColor blueColor] CGColor];
myShapeLayer.lineWidth = 2;
[self.view.layer addSublayer:myShapeLayer];
NSArray * boxsizes = @[
[NSValue valueWithCGSize:CGSizeMake(50, 50)],
[NSValue valueWithCGSize:CGSizeMake(50, 60)],
[NSValue valueWithCGSize:CGSizeMake(50, 70)]
];
CAKeyframeAnimation * anim1;
anim1=[CAKeyframeAnimation animationWithKeyPath:@"bounds.size"];
anim1.values = boxsizes;
NSMutableArray *paths = [NSMutableArray array];
for (NSValue *sizeValue in boxsizes) {
CGSize size = [sizeValue CGSizeValue];
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:(CGRect){.size = size}];
[paths addObject:(id)path.CGPath];
}
CAKeyframeAnimation * anim2;
anim2=[CAKeyframeAnimation animationWithKeyPath:@"path"];
anim2.values = paths;
CAAnimationGroup* group1 = [CAAnimationGroup animation];
group1.animations = [NSArray arrayWithObjects:anim1,anim2, nil];
group1.duration = 10.0;
[myShapeLayer addAnimation:group1 forKey:@"Somekeyhere"];
您可以看到,我使用大小来导出路径,因此有相同数量的关键帧点,并且它们都具有正确的大小
结果如下所示:
如何为“每个关键帧”设置一个新路径,并访问“该关键帧”的bounds.size值?是否有某种事件监听器,通过它我可以知道我处于哪个“关键帧”中?谢谢你的回答。我从问题中假设你知道如何使用CAKeyFrameAnimation?你能在问题中包含代码吗?这样我就知道我们在谈论同一件事了。我在这个问题的描述部分添加了代码。谢谢。好的,这正是我的建议,那么你现在的问题是如何制作两条环形路径?您的真实动画是否只有两个关键帧的值?我的问题是:由于anim1和anim2正在进行,anim1正在更改CAShapeLayer边界参数的高度,我们如何同步这两个动画,并确保anim2在通过anim1可用时拾取所需的高度。我希望我的问题是清楚的。谢谢