UILabel上的闪烁效果

28
我有一个背景颜色为灰色的UILabel。
我想让这个标签有一个闪烁的效果,就像它应该变成一点白色,然后变成灰色,直到我在程序中将它关闭为止。
有什么方法可以实现这个效果吗?

扩展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) }} - Erik Peruzzi
14个回答

57

您可以在代码块内完成此操作:

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

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


6
这是一个不错的选择,尽管它更像是一种缓慢的逐渐淡入淡出而不是眨眼。请注意,您可以使用 [self.yourLabel.layer removeAllAnimations] 停止动画。 - dvs
1
你可以在第一次设置 .alpha = 1;,然后在动画块中设置 .alpha = 0;,这样可以避免动画开始时出现奇怪的闪烁。 - Radu Ursache

34

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

如果我有一个名为“btn”的按钮,我该如何调用启动函数? - Oren Edrich
在按钮的操作中,您必须调用yourLabelName.startBlink()。 - Jaseem Abbas
1
不错的解决方案。运行得很好。 - GeRyCh

23

您可以很简单地创建一个扩展 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 })
    }
}

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

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

1
做了这个,但不知为何它不起作用。没有错误信息,只是无法运行。 - Jonathan Soifer

22

使用NSTimer

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

在您的闪烁函数中

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

你应该使用BOOL、YES和NO,而不是bool、TRUE和FALSE吗?谢谢。 - DCMaxxx
@DCMaxxx 是的。这是我早期(怀旧)iOS编程的一个坏习惯。现在已经改正了。感谢您指出。 - Krishnabhadra
3
提到不良实践,重复使用60fps的定时器...这应该是一个动画(当您不想在值之间进行任何插值时,存在离散动画)。 - David Rönnqvist
不错。UIView动画示例是通过淡化alpha值来实现的。OP想要一个闪烁效果。这个可以做到。 - Carl Hine

6
一种不同的方法,但有效。仅闪烁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)
    }
}

3
最好使用视图动画。这使得操作变得非常简单且易于控制。请尝试以下代码:
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将设置闪烁速度。此外,您可以将其用于任何继承自UIView的内容,例如按钮、标签、自定义视图等。


2
-(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;
}


2

在尝试使用多个选项时,可能会遇到困难,但是以下方法似乎可以很好地解决:

self.cursorLabel.alpha = 1
UIView.animate(withDuration: 0.7, delay: 0.0, options: [.repeat, .autoreverse, .curveEaseInOut], animations: {
    self.cursorLabel.alpha = 0
}, completion: nil)

2

修改Krishnabhadra的答案以获得更好的闪烁效果

声明一个类变量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;
    }
}

你应该在哪里使用计时器? - vishnu
嗨Vishnu,每当你想要标签开始闪烁时,请使用以下代码。 - Just Variable

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接