iOS UIView 上的闪烁动画次数有限

iOS blink animation on UIView with finite number of times

我正在尝试在 UIView 上创建闪烁效果。目前我正在使用无限次闪烁 UIView 的代码。代码看起来像这样

到目前为止我做了什么:

 func startBlink() {
                  UIView.animate(withDuration: 0.8,//Time duration
                                delay:0.0,
                                options:[.allowUserInteraction, .curveEaseInOut, .autoreverse, .repeat],
                                animations: { self.alpha = 0 },
                                completion: nil)
        }

但是此代码使 ui 视图无限次闪烁。我使用了另一个代码,但它只闪烁了一次。

我想要的:

So I am pretty close but I really want to blink the UIView for finite number of times i.e 30 times, and it must stop after 30th blink.

请帮助我,我想我的问题已经很清楚了。请帮帮我。

使用此函数为视图设置动画。希望能帮到你

extension UIView {  
        func flash(numberOfFlashes: Float) {
           let flash = CABasicAnimation(keyPath: "opacity")
           flash.duration = 0.2
           flash.fromValue = 1
           flash.toValue = 0.1
           flash.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
           flash.autoreverses = true
           flash.repeatCount = numberOfFlashes
           layer.add(flash, forKey: nil)
       }
 }

有一个内置的 class 函数用于计数并在块中调用它。

class func setAnimationRepeatCount(_ repeatCount: Float)

  func startBlink() {
              UIView.animate(withDuration: 0.8,//Time duration
                            delay:0.0,
                            options:[.allowUserInteraction, .curveEaseInOut,    .autoreverse, .repeat],
                            animations: { 

       UIView.setAnimationRepeatCount(30) // repeat 30 times.

     self.alpha = 0 
       },
                            completion: nil)
    }