如何显示 Swift 中单词之间有延迟的字符串?
How to display a string with a delay in between words in Swift?
所以我试过使用 Matt 的延迟代码,但它似乎不符合我的目的。我正在编写一个 Swift 应用程序,并希望能够显示一个字符串(一次说一个词),中间有延迟。有人有什么建议吗?
这将使您在两个词之间延迟四秒。您没有说在有或没有任何上下文的情况下执行此操作需要多少单词,或者您发布的任何代码都不容易理解您的要求。然而,这将在两个字符串之间提供延迟
self.mystring.text = "some word"
let seconds = 4.0
let delay = seconds * Double(NSEC_PER_SEC) // nanoseconds per seconds
var dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(dispatchTime, dispatch_get_main_queue(), {
// Performed with delay
self.mystring.text = "a different word"
})
class ViewController: UIViewController {
@IBOutlet weak var myTypeWriter: UITextView!
let words = "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda. Hello World !!!".componentsSeparatedByString(" ")
var counter = 0
var timer:NSTimer?
// create a method to add one word at a time to your text view
func addWord(){
// make sure you only update your text field if you have a corresponding item in your array
if counter < words.count {
myTypeWriter.text = "\(myTypeWriter.text) \(words[counter])"
// invalidate your timer when it is finished
} else {
timer?.invalidate()
}
counter++
}
override func viewDidLoad() {
super.viewDidLoad()
timer = NSTimer.scheduledTimerWithTimeInterval(0.25, target: self, selector: "addWord", userInfo: nil, repeats: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
所以我试过使用 Matt 的延迟代码,但它似乎不符合我的目的。我正在编写一个 Swift 应用程序,并希望能够显示一个字符串(一次说一个词),中间有延迟。有人有什么建议吗?
这将使您在两个词之间延迟四秒。您没有说在有或没有任何上下文的情况下执行此操作需要多少单词,或者您发布的任何代码都不容易理解您的要求。然而,这将在两个字符串之间提供延迟
self.mystring.text = "some word"
let seconds = 4.0
let delay = seconds * Double(NSEC_PER_SEC) // nanoseconds per seconds
var dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(dispatchTime, dispatch_get_main_queue(), {
// Performed with delay
self.mystring.text = "a different word"
})
class ViewController: UIViewController {
@IBOutlet weak var myTypeWriter: UITextView!
let words = "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda. Hello World !!!".componentsSeparatedByString(" ")
var counter = 0
var timer:NSTimer?
// create a method to add one word at a time to your text view
func addWord(){
// make sure you only update your text field if you have a corresponding item in your array
if counter < words.count {
myTypeWriter.text = "\(myTypeWriter.text) \(words[counter])"
// invalidate your timer when it is finished
} else {
timer?.invalidate()
}
counter++
}
override func viewDidLoad() {
super.viewDidLoad()
timer = NSTimer.scheduledTimerWithTimeInterval(0.25, target: self, selector: "addWord", userInfo: nil, repeats: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}