对UILabel的闪烁效果

Abh*_*nav 23 cocoa-touch objective-c uiview uilabel ios

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

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

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

Fle*_*ing 54

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

self.yourLabel.alpha = 1;
[UIView animateWithDuration:1.5 delay:0.5 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{
        self.yourLabel.alpha = 0;
} completion:nil];
Run Code Online (Sandbox Code Playgroud)

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

  • 这是一个不错的选择,虽然它比眨眼更缓慢淡入/淡出.请注意,您可以使用`[self.yourLabel.layer removeAllAnimations]`来停止动画. (6认同)

Jas*_*bas 26

斯威夫特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
    }
}
Run Code Online (Sandbox Code Playgroud)


Kri*_*dra 20

使用 NSTimer

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

在你的闪烁功能

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

  • @Krishnabhadra说到不好的做法.以60fps重复计时器...这应该是一个动画(当你不想在值之间进行任何插值时,存在离散动画) (3认同)

tig*_*ero 20

您可以简单地对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 })
    }
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特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 })
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑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 })
    }
}
Run Code Online (Sandbox Code Playgroud)


Abh*_*edi 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)
    }
}
Run Code Online (Sandbox Code Playgroud)