对UILabel的闪烁效果

我有一个背景颜色为灰色的UILabel。

我希望这个标签上有一个闪烁的效果,就像它应该变成一点白色然后变成灰色,它应该一直发生,直到我以编程方式关闭它。

任何线索如何实现这一目标?

使用NSTimer

 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)(1.0) target:self selector:@selector(blink) userInfo:nil repeats:TRUE]; BOOL blinkStatus = NO; 

在你的闪烁function

 -(void)blink{ if(blinkStatus == NO){ yourLabel.backgroundColor = [UIColor whiteColor]; blinkStatus = YES; }else { yourLabel.backgroundColor = [UIColor grayColor]; blinkStatus = NO; } } 

您可以在一个区块内执行此操作:

 self.yourLabel.alpha = 1; [UIView animateWithDuration:1.5 delay:0.5 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{ self.yourLabel.alpha = 0; } completion:nil]; 

所以你不需要第二种方法。

斯威夫特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 } } 

您可以简单地对UILabel类进行扩展,以支持闪烁效果。 我不认为使用计时器是一种正确的方法,因为你不会有任何淡入淡出效果。

以下是Swift的方法:

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

斯威夫特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 }) } } 

编辑Swift 3:适用于几乎所有视图

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

而是使用视图动画。 它使它非常简单并且易于控制。 尝试这个:

 self.yourLabel.alpha = 1.0f; [UIView animateWithDuration:0.12 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction animations:^{ self.yourLabel.alpha = 0.0f; } completion:^(BOOL finished){ // Do nothing }]; 

您可以调整值以获得不同的效果,例如,更改animateWithDuration将设置闪烁速度。 此外,您可以在任何inheritance自UIView示例的按钮,标签,自定义视图等中使用它。

调整Krishnabhadra答案以提供更好的眨眼效果

声明一个Class变量bool blinkStatus;

并粘贴下面给出的代码

 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; } } 
 -(void) startBlinkingLabel:(UILabel *)label { label.alpha =1.0f; [UIView animateWithDuration:0.32 delay:0.0 options: UIViewAnimationOptionAutoreverse |UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction |UIViewAnimationOptionBeginFromCurrentState animations:^{ label.alpha = 0.0f; } completion:^(BOOL finished){ if (finished) { } }]; } -(void) stopBlinkingLabel:(UILabel *)label { // REMOVE ANIMATION [label.layer removeAnimationForKey:@"opacity"]; label.alpha = 1.0f; } 

尝试使用swift并使用多个选项时遇到困难,但这看起来效果很好:

  self.cursorLabel.alpha = 1 UIView.animateWithDuration(0.7, delay: 0.0, options: [.Repeat, .Autoreverse, .CurveEaseInOut], animations: { self.cursorLabel.alpha = 0 }, completion: nil) 

这就是它对我有用的方式。 我改编了@flex_elektro_deimling的答案

第一个参数UIView.animateWithDuration是动画的总时间(在我的情况下,我将其设置为0.5),您可以在第一个和第二个(延迟)上设置不同的值以更改闪烁速度。

  self.YOURLABEL.alpha = 0; UIView.animateWithDuration( 0.5, delay: 0.2, options: UIViewAnimationOptions.Repeat | UIViewAnimationOptions.Autoreverse, animations: { self.YOURLABEL.alpha = 1 }, completion:nil) 

我的快速版基于Flex Elektro Deimling's答案 :

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

一个不同的approch但工作。 只闪烁3秒钟

 extension UIView { func blink() { let animation = CABasicAnimation(keyPath: "opacity") animation.isRemovedOnCompletion = false animation.fromValue = 1 animation.toValue = 0 animation.duration = 0.8 animation.autoreverses = true animation.repeatCount = 3 animation.beginTime = CACurrentMediaTime() + 0.5 self.layer.add(animation, forKey: nil) } } 
  int count; NSTimer *timer; timer= [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)(0.5) target:self selector:@selector(animationStart) userInfo:nil repeats:TRUE]; -(void)animationStart{ switch (count) { case 0: //205 198 115 count++; lbl.textColor=[UIColor colorWithRed:205.0f/255.0f green:198.0f/255.0f blue:115.0f/255.0f alpha:1]; break; case 1: count++; //205 198 115 56 142 142 lbl.textColor=[UIColor colorWithRed:56.0f/255.0f green:142.0f/255.0f blue:142.0f/255.0f alpha:1]; break; case 2: count++; //205 198 115 lbl.textColor=[UIColor colorWithRed:205.0f/255.0f green:205.0f/255.0f blue:0.0f/255.0f alpha:1]; break; case 3: count++; //205 198 115 84 255 159 lbl.textColor=[UIColor colorWithRed:84.0f/255.0f green:255.0f/255.0f blue:159.0f/255.0f alpha:1]; break; case 4: count++; //205 198 115 255 193 37 lbl.textColor=[UIColor colorWithRed:255.0f/255.0f green:193.0f/255.0f blue:37.0f/255.0f alpha:1]; break; case 5: count++; //205 198 115 205 200 177 lbl.textColor=[UIColor colorWithRed:205.0f/255.0f green:200.0f/255.0f blue:117.0f/255.0f alpha:1]; break; case 6: count++; //205 198 115 255 228 181 lbl.textColor=[UIColor colorWithRed:255.0f/255.0f green:228.0f/255.0f blue:181.0f/255.0f alpha:1]; break; case 7: count++; //205 198 115 233 150 122 lbl.textColor=[UIColor colorWithRed:233.0f/255.0f green:150.0f/255.0f blue:122.0f/255.0f alpha:1]; break; case 8: count++; //205 198 115 233 150 122 lbl.textColor=[UIColor colorWithRed:255.0f/255.0f green:200.0f/255.0f blue:200.0f/255.0f alpha:1]; break; case 9: count=0; //205 198 115 255 99 71 255 48 48 lbl.textColor=[UIColor colorWithRed:255.0f/255.0f green:48.0f/255.0f blue:48.0f/255.0f alpha:1]; break; default: break; } 

}

这是我在Swift 4.0中的解决方案,可以扩展任何UIVIew

 extension UIView{ func blink() { self.alpha = 0.2 UIView.animate(withDuration: 1, delay: 0.0, options: [.curveLinear, .repeat, .autoreverse], animations: { self.alpha = 1.0 }, completion: nil) } }