I have a UILabel with background color as grey.
I want a blinking effect on this label like it should become a little white & then become gray and it should keep
Tweaking Krishnabhadra Answer to give a better blink effect
Declare a Class variable bool blinkStatus;
And paste code given below
NSTimer *yourtimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)(10.0 / 60.0) target:self selector:@selector(blink) userInfo:nil repeats:TRUE];
blinkStatus = FALSE;
-(void)blink{
if(blinkStatus == FALSE){
yourLabel.hidden=NO;
blinkStatus = TRUE;
}else {
yourLabel.hidden=YES;
blinkStatus = FALSE;
}
}
My swift version based on Flex Elektro Deimling's answer:
private func startTimeBlinkAnimation(start: Bool) {
if start {
timeContainerView.alpha = 1
UIView.animateWithDuration(0.6, delay: 0.3, options:[.Repeat, .Autoreverse], animations: { _ in
self.timeContainerView.alpha = 0
}, completion: nil)
}
else {
timeContainerView.alpha = 1
timeContainerView.layer.removeAllAnimations()
}
}
Use NSTimer
NSTimer *timer = [NSTimer
scheduledTimerWithTimeInterval:(NSTimeInterval)(1.0)
target:self
selector:@selector(blink)
userInfo:nil
repeats:TRUE];
BOOL blinkStatus = NO;
in your blink function
-(void)blink{
if(blinkStatus == NO){
yourLabel.backgroundColor = [UIColor whiteColor];
blinkStatus = YES;
}else {
yourLabel.backgroundColor = [UIColor grayColor];
blinkStatus = NO;
}
}
Swift 3
extension UILabel {
func startBlink() {
UIView.animate(withDuration: 0.8,
delay:0.0,
options:[.allowUserInteraction, .curveEaseInOut, .autoreverse, .repeat],
animations: { self.alpha = 0 },
completion: nil)
}
func stopBlink() {
layer.removeAllAnimations()
alpha = 1
}
}
You can simply make an extension to the UILabel class that will support the blinking effect. I don't think using a timer is a right approach since you won't have any fade effect.
Here is the Swift way to do this:
extension UILabel {
func blink() {
self.alpha = 0.0;
UIView.animateWithDuration(0.8, //Time duration you want,
delay: 0.0,
options: [.CurveEaseInOut, .Autoreverse, .Repeat],
animations: { [weak self] in self?.alpha = 1.0 },
completion: { [weak self] _ in self?.alpha = 0.0 })
}
}
Swift 3:
extension UILabel {
func blink() {
self.alpha = 0.0;
UIView.animate(withDuration: 0.8, //Time duration you want,
delay: 0.0,
options: [.curveEaseInOut, .autoreverse, .repeat],
animations: { [weak self] in self?.alpha = 1.0 },
completion: { [weak self] _ in self?.alpha = 0.0 })
}
}
EDIT Swift 3: Works for almost any view
extension UIView {
func blink() {
self.alpha = 0.0;
UIView.animate(withDuration: 0.8, //Time duration you want,
delay: 0.0,
options: [.curveEaseInOut, .autoreverse, .repeat],
animations: { [weak self] in self?.alpha = 1.0 },
completion: { [weak self] _ in self?.alpha = 0.0 })
}
}
Got stuck when trying with swift and using multiple options but this seems to work nicely:
self.cursorLabel.alpha = 1
UIView.animate(withDuration: 0.7, delay: 0.0, options: [.repeat, .autoreverse, .curveEaseInOut], animations: {
self.cursorLabel.alpha = 0
}, completion: nil)