【问题标题】:Blinking effect on UILabelUILabel 上的闪烁效果
【发布时间】:2011-09-07 15:26:35
【问题描述】:

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

我想要在这个标签上产生闪烁效果,比如它应该变成一点白色然后变成灰色,并且它应该一直发生,直到我以编程方式将其关闭。

任何线索如何实现这一点?

【问题讨论】:

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

标签: ios objective-c cocoa-touch uiview uilabel


【解决方案1】:

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

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

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

【讨论】:

  • 这是一个不错的选择,尽管它更像是一个缓慢的淡入/淡出而不是眨眼。请注意,您可以使用 [self.yourLabel.layer removeAllAnimations] 停止动画。
  • 你可以.alpha = 1;第一次在动画块.alpha = 0;这将避免在动画开始时奇怪的闪烁。
【解决方案2】:

斯威夫特 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”的按钮,我将如何调用该函数来启动?
  • 在按钮的操作中,你必须调用 yourLabelName.startBlink()
  • 不错的解决方案。用作魅力
【解决方案3】:

使用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 是的。我早期(怀旧)的 iOS 编程不良做法之一。现已更正。感谢您注意到这一点。
  • @Krishnabhadra 说到不良做法。以 60fps 重复计时器...这应该是一个动画(当您不想在值之间进行任何插值时,会有离散动画)
  • 不错。 UIView 动画示例致力于淡化 alpha。 OP 想要一个 BLINK 效果。这样就可以了。
【解决方案4】:

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

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

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

【讨论】:

  • 做了这个,不知何故它不起作用。没有错误信息。就是不行。
【解决方案5】:

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

【讨论】:

    【解决方案6】:

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

    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 继承的任何东西上使用它,例如按钮、标签、自定义视图等。

    【讨论】:

      【解决方案7】:

      调整 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;
          }
      }
      

      【讨论】:

      • 你应该在哪里使用计时器?
      • 你好,当你想让标签开始闪烁时
      【解决方案8】:
      -(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;
      }
      

      【讨论】:

        【解决方案9】:

        尝试使用 swift 并使用多个选项时卡住了,但这似乎效果很好:

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

        【讨论】:

          【解决方案10】:

          这就是它对我的工作方式。我改编了@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)
          

          【讨论】:

            【解决方案11】:

            我的基于Flex Elektro Deimling's answer的swift版本:

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

            【讨论】:

              【解决方案12】:
                  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;
              }
              

              }

              【讨论】:

                【解决方案13】:

                这是我在 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)   
                    }
                }
                

                【讨论】:

                  【解决方案14】:

                  对于 Swift 3+,在此处所有出色答案的基础上,我最终进行了一些调整,使我获得了平滑的闪烁效果,在给定数量的循环后自动停止。

                  extension UIView {
                      func blink(duration: Double=0.5, repeatCount: Int=2) {
                          self.alpha = 0.0;
                          UIView.animate(withDuration: duration,
                              delay: 0.0,
                              options: [.curveEaseInOut, .autoreverse, .repeat],
                              animations: { [weak self] in
                                  UIView.setAnimationRepeatCount(Float(repeatCount) + 0.5)
                                  self?.alpha = 1.0
                              }
                          )
                      }
                  }
                  

                  【讨论】:

                    猜你喜欢
                    • 2012-04-05
                    • 1970-01-01
                    • 1970-01-01
                    • 2018-05-10
                    • 1970-01-01
                    • 1970-01-01
                    • 2019-01-05
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多