Blinking effect on UILabel

前端 未结 14 1833
野的像风
野的像风 2020-12-13 14:24

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

相关标签:
14条回答
  • 2020-12-13 14:53

    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.

    0 讨论(0)
  • 2020-12-13 14:53

    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)
    
    0 讨论(0)
  • 2020-12-13 14:54

    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)   
        }
    }
    
    0 讨论(0)
  • 2020-12-13 14:56

    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
                }
            )
        }
    }
    
    0 讨论(0)
  • 2020-12-13 15:03

    A different approch 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)
        }
    }
    
    0 讨论(0)
  • 2020-12-13 15:03
    -(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;
    }
    
    0 讨论(0)
提交回复
热议问题