标签中的动画文本 - 新单词似乎是读数



我想为我的标签做动画,以便单词每秒出现一个。就像新单词出现时一样,用户正在阅读。有人知道该怎么做,还是Github上有什么?

谢谢

正如@seyyedparsaneshaei所说,还有另一个答案可以给出"字幕"类型的滚动,但是,如果您想要的是一个单词,请考虑以下内容小素描将在操场上运行。它基本上设置了一个计时器,该计时器一次更改UILabel的文本。

import UIKit
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
class MyVC: UIViewController {
    var tickTock: Int = 0
    var myString = ""
    var label: UILabel!
    // Following block only needed in Playground. 
    // Normally you would wire up the label in IB.
    { didSet { self.view.addSubview(self.label) } }
    override func viewDidLoad() {
        // This next line only necessary in Playground. 
        // Normally, the view would be set from the StoryBoard
        self.view = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 100, height: 60)))
        super.viewDidLoad()
        // "1" in the next line is the inter-word interval in seconds.
        // Obviously you wouldn't hard-wire this.
        let _ = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { _ in
            let words = self.myString.components(separatedBy: " ")
            var word = "<Nothing>"
            if words.count > 0 {
                self.tickTock = self.tickTock % words.count
                if !words[self.tickTock].isEmpty {
                    word = words[self.tickTock]
                }
            }
            self.label?.text = word
            self.tickTock += 1
        })
    }
}
// Playground code to test:
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 60))
label.textColor = .yellow
label.textAlignment = .center
let myVC = MyVC()
myVC.myString = "May the source be with you"
myVC.label = label
PlaygroundPage.current.liveView = myVC.view

要查看动画,请从操场菜单中选择"查看>助手编辑>显示助手编辑"。

对于一个完整的解决方案,您可能想查看我使用过的components(separatedBy: CharactersSet)而不是components(separatedBy: String),这取决于您要如何显示标点符号...

相关内容

最新更新