我想为我的标签制作动画,以便每秒一个接一个地出现。它好像用户正在阅读新单词出现。 有谁知道怎么做,或者GitHub上有什么东西?
由于
答案 0 :(得分:0)
正如@SeyyedParsaNeshaei所说,还有另一个answer给出了“选框”类型滚动,但是,如果你想要的(如你所说)一次只有一个词,请考虑下面的小草图,将在Playground中运行。它基本上设置了一个计时器,可以一次更改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
要查看动画,请从Playground菜单中选择“View&gt; Assistant Editor&gt; Show Assistant Editor”。
要获得更全面的解决方案,您可能希望查看我使用的components(separatedBy: CharactersSet)
而不是components(separatedBy: String)
- 这取决于您想要如何呈现标点符号...