Swift UIView.animate在许多不同的字符串之间

时间:2018-10-11 05:28:46

标签: ios swift uiviewanimation

从新设备启动后,我希望在iPhone上的“欢迎”屏幕上重新创建动画-您在此处看到的用各种不同语言显示的问候语-https://www.youtube.com/watch?v=YZDbGyvAK7o

我的问题是我有大约20多个字符串要在它们之间淡入/淡出,但是我不相信创建20多个不同的UILabel是解决此问题的最有效方法。除了硬编码之外,还必须有另一种方法。

到目前为止,这是我对关键帧的了解

<to uri="iso:start" />

这也不起作用,因为文本不会更新,而是在模拟器上构建并运行时会看到数组中的最后一个字符串。

更新: 尽管我想避免使用计时器,但这似乎是唯一适用于我的方法。因此,我做了一个UIView扩展,它使用CATransitionView使标签褪色,您可以在stackoverflow上找到它的示例。然后,我创建了一个计时器,该计时器将运行淡入淡出动画并使用一个标签在字符串之间循环

3 个答案:

答案 0 :(得分:2)

您可以使用UIView.transition使用UILabel来使单个Timer中的文本淡入淡出,并使用override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.textLabel.text = self.strings[stringIndex] let _ = Timer.scheduledTimer(withTimeInterval: 3.0, repeats: true) { (timer) in self.stringIndex = (self.stringIndex + 1) % self.strings.count UIView.transition(with: self.textLabel, duration: 0.8, options: .transitionCrossDissolve, animations: { self.textLabel.text = "" }, completion: { (Void) in UIView.transition(with: self.textLabel, duration: 0.8, options: .transitionCrossDissolve, animations: { self.textLabel.text = self.strings[self.stringIndex] }, completion: nil) }) } } 来启动下一个动画:

$inputarray = array(
    array("id" => 2,"name" => "ProductName","comment" => "best product comment 1"),
    array("id" => 2,"name" => "ProductName","comment" => "best product comment 2"),
    array("id" => 2,"name" => "ProductName","comment" => "best product comment 3")
);

enter image description here

答案 1 :(得分:1)

您可以使用以下方法实现它:

    Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true) { (timer) in
        UIView.animate(withDuration: 1.0, animations: {
            self.label.alpha = 0
        }, completion: { (finished) in
            UIView.animate(withDuration: 1.0, animations: {
                self.label.alpha = 1.0

                let text = self.quotes.remove(at: 0)
                self.label.text = text
                self.quotes.insert(text, at: self.quotes.count-1)
            })
        })

    }

答案 2 :(得分:1)

@bigubosu这应该起作用:

var strings = ["1", "2", "3", "4", "5", "6"]

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    animate()
}

func animate() {
    guard strings.count > 0 else { return }

    let alpha:CGFloat = textLabel.alpha == 0 ? 1 : 0

    if alpha == 1 {
        textLabel.text = strings.remove(at: 0)
    }

    UIView.animate(withDuration: 2.0, animations: { [weak self] in
        self?.textLabel.alpha = alpha
    }, completion: { [weak self]  (finished) in
        self?.animate()
    })
}