UILabel 上的闪烁效果

发布于 2024-11-14 00:09:19 字数 120 浏览 2 评论 0原文

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

我想要这个标签上有闪烁效果,就像它应该变得有点白色和白色一样。然后变成灰色,它应该一直发生,直到我以编程方式将其关闭。

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

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 happen till I turn it off programatically.

Any clue how to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

来世叙缘 2024-11-21 00:09:19

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

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

因此您不需要第二种方法。

You can do this within a block:

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

So you dont need a second method.

从来不烧饼 2024-11-21 00:09:19

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

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
    }
}
蓝梦月影 2024-11-21 00:09:19

您只需对 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 })
    }
}

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 })
    }
}
雅心素梦 2024-11-21 00:09:19

使用 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;
   }
}

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;
   }
}
浅紫色的梦幻 2024-11-21 00:09:19

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

A different approach but works. Blinking only for 3 seconds

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)
    }
}
对岸观火 2024-11-21 00:09:19

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

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

Rather use the view animations. It makes it very simple and is easy to control. Try this:

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

You can tweak the values to get different effects for example, changing animateWithDuration wil set the blinking speed. Further you can use it on anything that inherits from UIView example a button, label, custom view etc.

忆离笙 2024-11-21 00:09:19

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

声明一个类变量 bool flashStatus;

并粘贴下面给出的代码

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

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;
    }
}
丑丑阿 2024-11-21 00:09:19
-(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;
}
-(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;
}
灵芸 2024-11-21 00:09:19

我的快速版本基于 Flex Elektro Deimling 的 答案

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

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()
    }
}
最丧也最甜 2024-11-21 00:09:19

在尝试使用 swift 并使用多个选项时陷入困境,但这似乎效果很好:

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

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)
半岛未凉 2024-11-21 00:09:19

这对我来说就是这样的。我改编了@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)

This is how it worked for me. I adapted the answer of @flex_elektro_deimling

First parameter UIView.animateWithDuration is the total time of the animation (In my case I've set it to 0.5), you may set different values on the first and second (delay) to change the blinking speed.

    self.YOURLABEL.alpha = 0;
    UIView.animateWithDuration(
        0.5, 
        delay: 0.2, 
        options: UIViewAnimationOptions.Repeat | UIViewAnimationOptions.Autoreverse, animations: {
            self.YOURLABEL.alpha = 1
        },
        completion:nil)
烟─花易冷 2024-11-21 00:09:19
    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;
}

}

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

}

油焖大侠 2024-11-21 00:09:19

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

Here is my solution in Swift 4.0 with extension for any 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)   
    }
}
心欲静而疯不止 2024-11-21 00:09:19

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

For Swift 3+, building on all the great answers here, I ended up with a few tweaks that gave me smooth blinking effect that automatically stops after a given number of cycles.

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
            }
        )
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文