UIView上的iOS眨眼动画次数有限



我正试图在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次闪烁后停止。

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

使用此函数为View设置动画。我希望它能帮助

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 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)
}

最新更新