动画 UIButton 边框厚度

animate UIButton border thickness

是否可以为 UIButton 的边框粗细设置动画?

我尝试了以下方法,但我只是在我试图设置动画的两个厚度之间快速闪烁。

//animate border thickness
    UIButton *btn = _barButton;
    CALayer *buttonLayer = [btn layer];

        [buttonLayer setBorderWidth:0.0f];
        [UIView animateWithDuration:1.0f animations:^{

            [buttonLayer setBorderWidth:15.0f];

        } completion:^(BOOL finished){

            [UIView animateWithDuration:1.0f animations:^{

                [buttonLayer setBorderWidth:2.5f];

            } completion:nil];
        }];

编辑:根据 David H 的建议,这可以通过 CABasicAnimation 完成。

UIButton *btn = _barButtons;
CALayer *buttonLayer = [btn layer];

//animate border thickness
CABasicAnimation *animation = [CABasicAnimation animation];
animation.keyPath = @"borderWidth";
animation.fromValue = @0.0;
animation.toValue = @5.0;
animation.duration = 0.25;
[buttonLayer addAnimation:animation forKey:@"basic"];

您可以做到,但您必须以不同的方式编写动画代码。查看 Apple 的 "Core Animation Programming Guide",找到有关 "CALayer Animatable Properties" 的部分。您可以看到 borderWidth 是可动画的,但您需要使用默认隐含的 CABasicAnimation 对象,如该文档中所述。

我过去做过这种事——但那是几年前的事了,所以对具体细节一头雾水。但是你可以让它正常工作。