Reputation: 93
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
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
Reputation: 4187
You would have to implement it
Upvotes: 0