如何在Swift中使用MarqueeLabel?

Swi*_*ift 4 xcode marquee ios swift tvos

我想知道是否有一种方法可以启用文本的水平滚动,即字幕类型文本.我使用过这个库:https://github.com/cbpowell/MarqueeLabel-Swift并将"MarqueeLabel.Swift"文件添加到我的应用程序中.但到目前为止,它并没有显示出我想要的效果.

这是我实现它的方式:

class ViewController: UIViewController 
{
@IBOutlet weak var marqueeLabel: MarqueeLabel! 
override func viewDidLoad() {
    super.viewDidLoad()
        self.marqueeLabel.tag = 101
        self.marqueeLabel.type = .Continuous
        self.marqueeLabel.speed = .Duration(5)
        self.marqueeLabel.animationCurve = .EaseInOut
        self.marqueeLabel.fadeLength = 10.0
        self.marqueeLabel.leadingBuffer = 30.0
        self.marqueeLabel.trailingBuffer = 20.0
        self.marqueeLabel.restartLabel()
 }
}
Run Code Online (Sandbox Code Playgroud)

我根据" MarqueeLabel Swift not working "中的解决方案在界面构建器中设置了自定义类.它仍然无法正常工作.

我得到的只是一个没有选框效果(或水平文字滚动)的标签.

PS:我也是iOS开发的新手.此外,我在实现此库之前尝试使用UIScrollView和UITextView.我不能使用UIWebView,因为我试图在TVOS中实现它.

我的问题是:

  1. 我在哪里错误的代码?

  2. 有没有其他方法可以使用Swift显示选框效果?

提前致谢.

Swi*_*ift 8

我用我的标签的这一小段代码滚动像字幕:

@IBOutlet weak var firstLabel: UILabel! 
override func viewDidLoad() {
    super.viewDidLoad()

    UIView.animateWithDuration(12.0, delay: 1, options: ([.CurveLinear, .Repeat]), animations: {() -> Void in
            self.firstLabel.center = CGPointMake(0 - self.firstLabel.bounds.size.width / 2, self.firstLabel.center.y)
            }, completion:  { _ in })
}
Run Code Online (Sandbox Code Playgroud)

是的,它奏效了.

  • 它没有按预期工作,它为整个 UILabel 提供动画,而不是其中的文本。 (2认同)
  • 很酷,对于移动 UILabel 很有用。但根本不是一个选取框类型的实现。 (2认同)

Bri*_*ian 6

简单的选取框 - Swift 4.0:

override func viewDidLoad() {
    super.viewDidLoad()
    marqueeLabel.text = " text text here!"
    _ = Timer.scheduledTimer(timeInterval: 0.3, target: self, selector: #selector(ViewController.marquee), userInfo: nil, repeats: true)
}
extension ViewController {
  @objc func marquee(){

    let str = marqueeLabel.text!
    let indexFirst = str.index(str.startIndex, offsetBy: 0)
    let indexSecond = str.index(str.startIndex, offsetBy: 1)
    marqueeLabel.text = String(str.suffix(from: indexSecond)) + String(str[indexFirst])

  }
}
Run Code Online (Sandbox Code Playgroud)


iAj*_*iAj 5

为 Swift 3 更新,无需使用任何第三方库或 pod

import UIKit

class ViewController: UIViewController {

    let redLabel: UILabel = {

        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = .boldSystemFont(ofSize: 16)
        label.textColor = .red
        label.text = "The first paragraph of the body should contain the strongest argument, most significant example, cleverest illustration, or an obvious beginning point."
        return label
    }()

    let greenLabel: UILabel = {

        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = .systemFont(ofSize: 14)
        label.textColor = .green
        label.text = "The second paragraph of the body should contain the second strongest argument, second most significant example, second cleverest illustration, or an obvious follow up the first paragraph in the body."
        return label
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "Marquee Label Demo"
        view.backgroundColor = .white

        view.addSubview(redLabel)
        view.addSubview(greenLabel)

        setupAutoLayout()
        startMarqueeLabelAnimation()
    }

    func startMarqueeLabelAnimation() {

        DispatchQueue.main.async(execute: {

            UIView.animate(withDuration: 10.0, delay: 1, options: ([.curveLinear, .repeat]), animations: {() -> Void in

                self.redLabel.center = CGPoint(x: 0 - self.redLabel.bounds.size.width / 2, y: self.redLabel.center.y)
                self.greenLabel.center = CGPoint(x: 0 - self.greenLabel.bounds.size.width / 2, y: self.greenLabel.center.y)

            }, completion:  nil)
        })
    }

    func setupAutoLayout() {

        redLabel.leftAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        redLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
        redLabel.heightAnchor.constraint(equalToConstant: 20).isActive = true

        greenLabel.leftAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        greenLabel.topAnchor.constraint(equalTo: redLabel.bottomAnchor, constant: 50).isActive = true
        greenLabel.heightAnchor.constraint(equalToConstant: 20).isActive = true
    }
}
Run Code Online (Sandbox Code Playgroud)

特别鸣谢:

/sf/users/581269671/

/sf/users/587220441/

单击此处查看演示1