如何在Swift中使用选取框function?

我想知道是否有一种方法来启用水平滚动的文字即字幕types的文字。 我已经使用这个库: 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() } } 

我已经根据“ MarqueeLabel Swift not working ”中的解决scheme在界面生成器中设置了自定义类。 它仍然不起作用。

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

PS:我也是iOS开发新手。 另外,我试图在实现这个库之前使用UIScrollView和UITextView。 我不能使用UIWebView,因为我试图在TVOS中实现这个。

我的问题是:

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

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

提前致谢。

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

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

是的,它的工作。

在SWIFT 3中:

 @IBOutlet weak var YOURLABEL: UILabel! override func viewDidLoad() { super.viewDidLoad() UIView.animate(withDuration: 12.0, delay: 1, options: ([.curveLinear, .repeat]), animations: {() -> Void in self.YOURLABEL.center = CGPoint(x: 0 - self.YOURLABEL.bounds.size.width / 2, y: self.YOURLABEL.center.y) }, completion: { _ in }) } 

更新了Swift 3,不使用任何第三方库或豆荚

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

特别感谢:

https://stackoverflow.com/users/8303852/kartik-patel

https://stackoverflow.com/users/8388863/sid-patel

点击这里看演示1