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

标签 ios xcode swift

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

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

Red
Blue
Green

I want the label to scroll through these.

For a better idea, here is what I currently have;

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 implementation you can use .

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

关于ios - 学习 Swift 并尝试在标签中创建字符串循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31692338/

相关文章:

ios - 自定义单元格不显示 PFImageView?

ios - [__NSCFDictionary enumerateObjectsUsingBlock :]: unrecognized selector sent to instance 0x101918a10'

ios - 在 UITableView 的自定义 Prototype Cell 中,UITextField 值替换为其他 Prototype Cell

swift - 存在后退按钮时无法设置 UINavigationBar 标题或将 UIButtons 放置在 UINavigationBar 中

swift - SpriteKit 。停止使用 CameraNode 旋转纹理

ios - 单击它后如何隐藏 ui 按钮并在该按钮 swift 的位置显示另一个按钮?

ios - Swift:如何获取 SKShapeNode 的位置?

iOS:检测包含多个图像的 UIImageView 中的图像触摸

ios - -[UIViewController tableView :numberOfRowsInSection:]: unrecognized selector sent to instance 0x7ff437517770

iOS 7+ 部分滑动返回问题