iosswiftbuttontextblink

Make label text "blink" or "flash" on separate button press in Swift 3


Given an app with an on-screen text label and a button. Every time the button is pressed, the text changes. I want to be able to press the button and the text will "blink" when changing text, to make the text change more apparent to the user.

Given the following variables:

@IBOutlet weak var text: UILabel!
@IBAction func buttons(_ sender: UIButton) {}

I've tried SKAction, fadein/fadeout but all tutorials/help are in older versions of Swift and aren't working for me.


Solution

  • Swift 3.0
    One of the method to blink of UIView is achieved by UIKit animations.

    @IBOutlet var label: UILabel!
    
    var x = 0
    let text = ["Vanakkam","Hello","Hi","Hola","Ni Hao","Oi","Namastae"]
    
    @IBAction func buttonAction(_ sender: UIButton) {
    
        UIView.animate(withDuration: 0.2, animations: { 
            self.label.alpha = 0.0
            }) { (bool) in
                self.label.alpha = 1.0
                self.label.text = text[self.x]
                if self.x < text.count{
                    self.x = self.x+1
                }else{
                    self.x = 0
                }
        }
    }
    

    OUTPUT:- enter image description here