学习Swift并尝试在标签

时间:2015-07-29 06:02:54

标签: ios xcode swift

在Swift中,我试图以1-2秒的间隔自动循环标签中的一系列字符串。

例如,我在数组中有以下字符串:

Red
Blue
Green

我希望标签滚动浏览这些。

为了更好的主意,这就是我现在所拥有的;

import UIKit

class ViewController: UIViewController {

    @IBOutlet var Label: UILabel!

    @IBOutlet var Label2: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func RandomWord() {
        var RandomWord = arc4random_uniform(5)
        switch (RandomWord) {
        case 0:
            Label.text = "Blue"
            if Label.text == "Blue" {
                Label.textColor = UIColor.blueColor()
            }
            break;
        case 1:
            Label.text = "Red"
            if Label.text == "Red" {
                Label.textColor = UIColor.redColor()
            }
            break;
        case 2:
            Label.text = "Green"
            if Label.text == "Green" {
                Label.textColor = UIColor.greenColor()
            }
            break;
        case 3:
            Label.text = "Yellow"
            if Label.text == "Yellow" {
                Label.textColor = UIColor.yellowColor()
            }
            break;
        case 4:
            Label.text = "Orange"
            if Label.text == "Orange" {
                Label.textColor = UIColor.orangeColor()
            }
            break;
        default:
            break;

        }
    }

    @IBAction func RandomNumber() {
        var RandomNumber = arc4random_uniform(100)
        Label2.text = String(RandomNumber)
    }

}

我想删除对按钮的需要,并让标签循环通过字符串。关于最佳解决方案的任何想法?

2 个答案:

答案 0 :(得分:1)

此类文本滚动/抓取功能未自动内置到UILabel中。您可以自己实现它(这不是最微不足道的事情),或者您可以利用其他人在某个开源项目中所做的工作。

例如,我刚发现MarqueeLabel确实有Swift implementation you can use

尝试将其添加到您的项目中,看看是否能满足您的需求。

答案 1 :(得分:1)

如果我理解你的问题,你正在寻找类似下面的东西,在定时间隔,选择随机颜色用于填充标签的文字和文字颜色:

numpy
相关问题