繁体   English   中英

动画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的“核心动画编程指南”,然后找到“ CALayer动画属性”部分。 您可以看到borderWidth是可动画的,但是您需要使用该文档中介绍的默认隐式CABasicAnimation对象。

我过去做过这种事情-但是几年前在具体细节上还很模糊。 但是您可以确保它正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM