I'm trying to achieve a blinking/flashing effect with some duration for a UIImageView in my iOS app. I want the UIImageView to blink/flash with alpha, not with different images.
I have another code-snippet from an android app which sort of describe what I'm trying to achieve in XCode Swift. I post it here below:
This is what I have achieved so far, in XCode Swift:
ellipses.alpha=0.8
ellipses.animationDuration=1
ellipses.animationRepeatCount=0
ellipses.startAnimating()
And of course, it's not working! I'm not really sure on how to achieve this, and would be very happy to get some guidance.
This code below is for android, not for Swift/Xcode (But it describes what my goal is pretty clear)
Animation animation = new AlphaAnimation(1, 0); //to change visibility from visible to invisible
animation.setDuration(1000); //1 second duration for each animation cycle
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatCount(Animation.INFINITE); //repeating indefinitely
animation.setRepeatMode(Animation.REVERSE); //animation will start from end point once ended.
imageButton.startAnimation(animation); //to start animation
You could animate the opacity of the image view repeatedly. Something like:
UIView.animate(withDuration: 2.0,
delay: 0,
options: [.repeat, .autoreverse],
animations: { imageView.alpha = 0 }
)
Use the following code to get image animation like a blink.
Image Blink (Using image view hide/show):-
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.alarmAlertActivate), userInfo: nil, repeats: true)
}
@objc func alarmAlertActivate(){
myImage.isHidden = !myImage.isHidden
}
Image Blink (Using image view alpha):-
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Timer.scheduledTimer(timeInterval: 0.7, target: self, selector: #selector(self.alarmAlertActivate), userInfo: nil, repeats: true)
}
@objc func alarmAlertActivate(){
UIView.animate(withDuration: 0.7) {
self.alarmImage.alpha = self.alarmImage.alpha == 1.0 ? 0.0 : 1.0
}
}
I wanted to make my button blink/flash abruptly, not smoothly fade in and out as most IOS animation do by default. The accepted answer above does this with a timer, but you can set up the regular animator class to do this as well -- without smoothing. Below is an example that blinks the border color of a button, but this can be used on anything that requires the same effect.
The trick here is to use "keyframe" animation, and set calculationMode to "discrete":
let color = CAKeyframeAnimation(keyPath: "borderColor")
color.values = [CGColor.white, CGColor.black, CGColor.white]
color.keyTimes = [0, 0.5, 1]
color.duration = 1
color.repeatCount = .infinity
color.calculationMode = .discrete
gotItButton.layer.borderWidth = 3
gotItButton.layer.add(color, forKey: "")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With