(iOS)如何使用shapeLayer为圆角矩形设置动画?

mur*_*b83 5 animation core-animation shape cashapelayer ios

我试图动画圆角矩形的宽度,问题是当从更大的宽度变为更宽的宽度时,动画会进行"像差轻松跳跃".

这是代码:

shapeLayer = [CAShapeLayer layer];
shapeRect = CGRectMake(0.0f, 0.0f, 150.0f, 200.0f);
[shapeLayer setBounds:shapeRect];
[shapeLayer setPosition:CGPointMake(iniPosX, 80.0f)];
[shapeLayer setFillColor:[[UIColor blackColor] CGColor]];
[shapeLayer setStrokeColor:[[UIColor clearColor] CGColor]];
[shapeLayer setLineWidth:1.0f];
[shapeLayer setLineJoin:kCALineJoinRound];
[shapeLayer setOpacity:0.2];
path = [UIBezierPath bezierPathWithRoundedRect:shapeRect cornerRadius:15.0];
[shapeLayer setPath:path.CGPath];
[self.layer addSublayer:shapeLayer];
Run Code Online (Sandbox Code Playgroud)

当我开始动画时:

- (void)adjustSelectorToPosAndSize:(float)posX andWidth:(float)width
{
    shapeRect = CGRectMake(0.0f, 0.0f, width, 200.0f);
    [shapeLayer setBounds:shapeRect];
    [shapeLayer setPosition:CGPointMake(posX, 80.0f)];
    path = [UIBezierPath bezierPathWithRoundedRect:shapeRect cornerRadius:15.0];
    [shapeLayer setPath:path.CGPath];
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Dav*_*ist 3

如果您仅将它用于圆角矩形,那么使用 CAShapeLayer 执行此操作对我来说似乎过于复杂。为什么不直接使用正确设置cornerRadiusCALayer呢?

使用cornerRadius设置对框架进行动画处理可以正常工作。

myLayer = [CALayer layer];
shapeRect = CGRectMake(0.0f, 0.0f, 150.0f, 200.0f);
[myLayer setBounds:shapeRect];
[myLayer setPosition:CGPointMake(iniPosX, 80.0f)];
[myLayer setBackgroundColor:[[UIColor blackColor] CGColor]];
[myLayer setBorderColor:[[UIColor clearColor] CGColor]];
[myLayer setBorderWidth:1.0f];
[myLayer setOpacity:0.2];
[myLayer setCornerRadius:15.0];
[self.layer addSublayer:myLayer];
Run Code Online (Sandbox Code Playgroud)

通过简单地链接框架(而不是路径)来对其进行动画处理

- (void)adjustSelectorToPosAndSize:(float)posX andWidth:(float)width
{
    shapeRect = CGRectMake(0.0f, 0.0f, width, 200.0f);
    [myLayer setBounds:shapeRect];
    [myLayer setPosition:CGPointMake(posX, 80.0f)];
}
Run Code Online (Sandbox Code Playgroud)
重要的:

一些属性更改了名称,例如“ fillColorbecome backgroundColor”、“the”strokeColorlineWidth“become borderColorborderWidth等。