Blinking effect on UILabel

前端 未结 14 1957
野的像风
野的像风 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条回答
  •  Happy的楠姐
    2020-12-13 14:49

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

提交回复
热议问题