Reputation: 77
I am trying to create a blinking effect on the UIView. Currently I am using a code which blinks the UIView with infinite number of times. the Code looks like this
WhatI have done so far:
func startBlink() {
UIView.animate(withDuration: 0.8,//Time duration
delay:0.0,
options:[.allowUserInteraction, .curveEaseInOut, .autoreverse, .repeat],
animations: { self.alpha = 0 },
completion: nil)
}
But this code blinks the ui view for infinite number of time. I used another code but that was blinking for one time only.
What I want:
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.
Please help me in this, I think I have clear in my question. Please help me out.
Upvotes: 6
Views: 6113
Reputation: 11539
There is a builtin in class function for the count and call it in the block.
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)
}
Upvotes: 1
Reputation: 482
Use this function to animate View. I hope it can help
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)
}
}
Upvotes: 12