abdo
abdo

Reputation: 93

Change text from UITextView in a certain amount of time

I have a UITextView and what I am trying to do is to add a different text after 2 seconds. So every 2 seconds the text will disappear and another text will appear. I have done that with only 2 texts but what if I want to have more than that.

The code below will show you how I did it:

@IBOutlet weak var label: UILabel!

var isTextOne = true
var textOne: String = "one"
var textTwo: String = "two"
var textThree: String = "three"

@IBOutlet weak var quoteView: UIView!

var textTimer: Timer!

func toggleText() {
    label.text = isTextOne ? textTwo:textOne
    isTextOne = !isTextOne
    fadeViewInThenOut(view: quoteView, delay: 2)
}

func fadeViewInThenOut(view : UIView, delay: TimeInterval) {
    let animationDuration = 0.25

    // Fade in the view
    UIView.animate(withDuration: animationDuration, animations: { () -> Void in
        view.alpha = 1
    }) { (Bool) -> Void in
        // After the animation completes, fade out the view after a delay

        UIView.animate(withDuration: animationDuration, delay: delay, options: .curveEaseInOut, animations: { () -> Void in
            view.alpha = 0
        }, completion: nil)
    }
}

Upvotes: 0

Views: 79

Answers (2)

abdo
abdo

Reputation: 93

this is how i did it.

var Quote: String = ""


    var gameTimer: Timer!
    var textTimer: Tr!ime

    func fadeViewInThenOut(view : UIView, delay: TimeInterval) {

            let animationDuration = 0.25

            // Fade in the view
            UIView.animate(withDuration: animationDuration, animations: { () -> Void in
                view.alpha = 1
            }) { (Bool) -> Void in

                // After the animation completes, fade out the view after a delay

                UIView.animate(withDuration: animationDuration, delay: delay, options: .curveEaseInOut, animations: { () -> Void in
                    view.alpha = 0
                },
                            completion: nil)
            }
        }

        func runTimedCode() {

            fadeViewInThenOut(view: quoteView, delay: 0.7)


            func randomNumber(_ arrayLength: Int) -> Int {
                let unsignedArrayCount = UInt32(arrayLength)
                let unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
                let randomNumber = Int(unsignedRandomNumber)


                return randomNumber
            }

            // Importing Quotes plist File
            let quotes = ImportList(FileName: "QuotesList")

            // Selects Quote
            let chosenQuote: String = quotes.array[randomNumber(quotes.count())] as! String

            // Assigns Quote & Author to IBOutlet
            Quote = chosenQuote

            label.text = Quote

        }

and i put this in view did load

gameTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(runTimedCode), userInfo: nil, repeats: true)

Upvotes: 1

JRG
JRG

Reputation: 4187

You would have to implement it

  1. Create a list of words that will hold all text/words that needs to be displayed in text view
  2. Create a method that returns a random number between a range [0-(n-1) where n is the total size of the list of words to be displayed]
  3. Every 2 seconds or fixed interval, call the above method to give you random number which can be used to get the word at that location in the list.

Upvotes: 0

Related Questions