如何在Swift中使用marquee功能? [英] How to use marquee feature in Swift?
问题描述
我想知道是否有一种方法可以启用文本的水平滚动,即字幕类型文本。我使用过这个库: https://github.com/cbpowell/MarqueeLabel-Swift 和将MarqueeLabel.Swift文件添加到我的应用程序中。但到目前为止,它并没有显示我想要的效果。
I want to know if there's a way to enable horizontal scrolling of text i.e., marquee type text. I have used this library: https://github.com/cbpowell/MarqueeLabel-Swift and added the "MarqueeLabel.Swift" file to my application. But so far, it doesn't show the marquee effect that I wanted.
这就是我实现它的方式:
This is how I implemented it:
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中的解决方案在界面构建器中设置了自定义类不工作
。它仍然不起作用。
I have set the custom class in the interface builder according to the solution in "MarqueeLabel Swift not working" . It still doesn't work.
我得到的只是一个没有选框效果(或水平文字滚动)的标签。
All I get is just a label displaying without the marquee effect (or horizontal text scroll).
PS:我也是iOS开发的新手。此外,我在实现此库之前尝试使用UIScrollView和UITextView。我不能使用UIWebView,因为我正试图在TVOS中实现它。
P.S: I am new to iOS development too. Also, I tried using UIScrollView and UITextView before implementing this library. And I cannot use UIWebView as I'm trying to implement this in TVOS.
我的问题是:
-
我的代码出了什么问题?
Where did I go wrong with the code?
有没有其他方法可以使用Swift显示选框效果?
Is there an alternative way to display marquee effect using Swift?
提前致谢。
推荐答案
我使用这段代码将我的标签滚动为字幕:
I used this little piece of code for my label to scroll like marquee:
@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中使用marquee功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!