繁体   English   中英

学习Swift并尝试在标签中创建字符串循环

[英]Learning Swift and Trying to Create a String Loop in a Label

在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)
    }

}

我想消除对按钮的需要,只需要使标签遍历字符串即可。 关于最佳解决方案有什么想法吗?

UILabel不会自动内置这种文本滚动/爬网功能。 您可以自己实现它(这不是最琐碎的事情),也可以利用其他人在某个开源项目中所做的工作。

例如,我刚刚发现MarqueeLabel确实具有可以使用Swift实现

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

如果我正确理解了您的问题,则您正在寻找类似以下内容的内容,其中在一定时间间隔内,选择了一种随机颜色,用于填充标签的文本和文本颜色:

struct ColorData {
    let name: String
    let value: UIColor
}

class MyViewController: UIViewController {

    let data = [
        ColorData( name: "Blue", value: UIColor.blueColor() ),
        ColorData( name: "Red", value: UIColor.redColor() ),
        ColorData( name: "Green", value: UIColor.greenColor() )
    ]

    @IBOutlet weak var label: UILabel!

    var timer = NSTimer()

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear( animated )

        self.timer = NSTimer.scheduledTimerWithTimeInterval( 1.0, target: self, selector: "update", userInfo: nil, repeats: true )
    }

    func update() {
        let randomIndex = Int(arc4random() % UInt32(self.data.count))
        let colorData = data[ randomIndex ]
        self.label.text = colorData.name
        self.label.textColor = colorData.value
    }

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear( animated )

        // Don't forget to invalidate, otherwise this instance of `MyViewController` will leak
        self.timer.invalidate()
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM