【问题标题】:Swift Change circular border color with animation用动画快速改变圆形边框颜色
【发布时间】:2018-05-21 09:20:50
【问题描述】:

我使用 swift 创建了一个圆形按钮,我想做的是用动画改变边框颜色

我使用了以下代码:

let color = CABasicAnimation(keyPath: "borderColor")
color.fromValue = UIColor.black.cgColor
color.toValue = UIColor.red.cgColor
color.duration = 1
color.repeatCount = 1
sender.layer.add(color, forKey: "color and width")

边框颜色正在改变,但没有给出想要的效果,它一次改变了边框颜色。我想要的效果就像您在旧颜色上绘制一样,保持简单 ==> 就像一个进度条,您可以看到旧颜色消失并被新颜色取代。

有没有办法做到这一点?

感谢您的帮助。

更新代码

var storkeLayer = CAShapeLayer()

@IBOutlet weak var Mybut: UIButton!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    storkeLayer.fillColor = UIColor.clear.cgColor
    storkeLayer.strokeColor = UIColor.black.cgColor
    storkeLayer.lineWidth = 2

    // Create a rounded rect path using button's bounds.

    storkeLayer.path = CGPath.init(roundedRect: Mybut.bounds, cornerWidth: Mybut.frame.width / 2 , cornerHeight: Mybut.frame.height / 2, transform: nil)


    // Add layer to the button
    Mybut.layer.addSublayer(storkeLayer)
}


 @IBAction func bytton(_ sender: UIButton) {

    storkeLayer.fillColor = UIColor.clear.cgColor
    storkeLayer.strokeColor = UIColor.red.cgColor
    storkeLayer.lineWidth = 2

    // Create a rounded rect path using button's bounds.

    storkeLayer.path = CGPath.init(roundedRect: Mybut.bounds, cornerWidth: Mybut.frame.width / 2 , cornerHeight: Mybut.frame.height / 2, transform: nil)


    // Add layer to the button
    sender.layer.addSublayer(storkeLayer)

    // Create animation layer and add it to the stroke layer.
    let animation = CABasicAnimation(keyPath: "strokeEnd")
    animation.fromValue = CGFloat(0.0)
    animation.toValue = CGFloat(1.0)
    animation.duration = 1
   animation.fillMode = kCAFillModeForwards

   // animation.fillMode = kCAFillModeBackwards
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    storkeLayer.add(animation, forKey: "circleAnimation")



}

【问题讨论】:

标签: ios swift uibutton caanimation


【解决方案1】:

您可以使用以下代码 sn-p 来描边按钮的路径。

@IBAction func buttonTapped(_ sender: UIButton) {
    let storkeLayer = CAShapeLayer()
    storkeLayer.fillColor = UIColor.clear.cgColor
    storkeLayer.strokeColor = UIColor.red.cgColor
    storkeLayer.lineWidth = 2

    // Create a rounded rect path using button's bounds.
    storkeLayer.path = CGPath.init(roundedRect: sender.bounds, cornerWidth: 5, cornerHeight: 5, transform: nil) // same path like the empty one ...
    // Add layer to the button
    sender.layer.addSublayer(storkeLayer)

    // Create animation layer and add it to the stroke layer.
    let animation = CABasicAnimation(keyPath: "strokeEnd")
    animation.fromValue = CGFloat(0.0)
    animation.toValue = CGFloat(1.0)
    animation.duration = 1
    animation.fillMode = kCAFillModeForwards
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    storkeLayer.add(animation, forKey: "circleAnimation")
}

这个 sn-p 创建了类似这样的东西:

【讨论】:

  • 谢谢@HAK,您的代码适用于标准的UIbutton,在我的情况下,我有圆形按钮的圆角半径,我该如何管理?非常感谢您的帮助
  • 增加上面CGPath.init方法中的cornerWidth和cornerHeight,使其更圆。
  • 好的,谢谢,我还有一些小东西,当我加载视图时,当我单击按钮将边框颜色更改为红色时,Strokecolor 是黑色的动画 ==> 动画工作非常好,但黑色消失而不是褪色。我该如何解决?上面更新的代码
  • 我使用第二个 storkeLayer 解决了这个问题。谢谢你一百万次
  • 太棒了。谢谢! :)
猜你喜欢
  • 2014-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-05
  • 1970-01-01
  • 2013-04-29
相关资源
最近更新 更多