6

我正在尝试在 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 视图闪烁了无数次。我使用了另一个代码,但它只闪烁了一次。

我想要的是:

所以我非常接近,但我真的想闪烁 UIView 有限次数,即 30 次,它必须在第 30 次闪烁后停止。

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

4

2 回答 2

10

使用此功能为视图设置动画。我希望它可以帮助

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)
       }
 }
于 2018-11-06T12:53:45.160 回答
1

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

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)
    }
于 2018-11-06T14:01:30.017 回答