Created
July 9, 2020 11:34
-
-
Save wassim93/9e9e6a993730f8d687b3d1a2ebb66077 to your computer and use it in GitHub Desktop.
text animation in label forward and backward swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var countries = ["'Espagne","'Italie","aFrance"] | |
var cIndex = 0 | |
var initStringLength : Int? = 0 | |
override func viewDidAppear(_ animated: Bool) { | |
titleLabel.text = "#CetEteJeVisiteL" | |
initStringLength = titleLabel.text?.count | |
animateText(countries[cIndex]) | |
} | |
fileprivate func animateText(_ country: String) { | |
let countryLength = country.count | |
for i in country { | |
if i != country.last{ | |
animateForward(i) | |
}else{ | |
animateForward(i) | |
Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { _ in | |
if self.animateBack(countryLength) { | |
if self.cIndex >= 2 { | |
self.cIndex = 0 | |
} else { | |
self.cIndex += 1 | |
} | |
self.animateText(self.countries[self.cIndex]) | |
} | |
} | |
} | |
} | |
} | |
fileprivate func animateBack(_ countryLength: Int) -> Bool{ | |
var animFinished:Bool = false | |
for _ in 0...countryLength { | |
self.titleLabel.text = remove_char(str1: self.titleLabel.text!) | |
RunLoop.current.run(until: Date()+0.20) | |
} | |
animFinished = true | |
return animFinished | |
} | |
fileprivate func animateForward(_ i: String.Element) { | |
titleLabel.text! += "\(i)" | |
RunLoop.current.run(until: Date()+0.20) | |
} | |
func remove_char(str1: String) -> String { | |
var s = str1 | |
if let index = s.index(s.startIndex, offsetBy: str1.count-1, limitedBy: s.endIndex) { | |
if s.count != initStringLength { | |
s.remove(at: index) | |
} | |
} else { | |
print("is out of range") | |
} | |
return s | |
} | |
Author
wassim93
commented
Jul 9, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment