xcode 如何在 Swift 中使用 MarqueeLabel?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35172267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use MarqueeLabel in Swift?
提问by 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-Swiftand added the "MarqueeLabel.Swift" file to my application. But so far, it doesn't show the marquee effect that I wanted.
我想知道是否有办法启用文本的水平滚动,即选取框类型的文本。我使用了这个库:https: //github.com/cbpowell/MarqueeLabel-Swift并将“MarqueeLabel.Swift”文件添加到我的应用程序中。但到目前为止,它并没有显示出我想要的字幕效果。
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()
}
}
I have set the custom class in the interface builder according to the solution in "MarqueeLabel Swift not working" . It still doesn't work.
我已经根据“ MarqueeLabel Swift not working”中的解决方案在界面构建器中设置了自定义类。它仍然不起作用。
All I get is just a label displaying without the marquee effect (or horizontal text scroll).
我得到的只是一个没有选取框效果(或水平文本滚动)的标签显示。
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.
PS:我也是 iOS 开发的新手。另外,我在实现这个库之前尝试使用 UIScrollView 和 UITextView 。我无法使用 UIWebView,因为我试图在 TVOS 中实现它。
My questions are:
我的问题是:
Where did I go wrong with the code?
Is there an alternative way to display marquee effect using Swift?
我的代码哪里出错了?
有没有其他方法可以使用 Swift 显示选取框效果?
Thanks in advance.
提前致谢。
回答by 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 })
}
Yes, it worked.
是的,它奏效了。
回答by iAj
Updated for Swift 3without using any third party library or pods
为 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
}
}
Special Thanks:
特别感谢:
https://stackoverflow.com/users/8303852/kartik-patel
https://stackoverflow.com/users/8303852/kartik-patel
https://stackoverflow.com/users/8388863/sid-patel
https://stackoverflow.com/users/8388863/sid-patel
click here to see demo 1
单击此处查看演示1
回答by Brian
Simple marquee - Swift 4.0:
简单的选框 - 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])
}
}
回答by pixelCss
In SWIFT 3:
在 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 })
}
回答by Michele Dall'Agata
Since the original question was related to tvOS, I have just found out by mistake (after weeks of searching!) that tvOS 12 betahas implemented this:
由于最初的问题与 tvOS 有关,我刚刚错误地发现(经过数周的搜索!)tvOS 12 beta已经实现了这个:
One gets the marquee effect only when the ancestor is focused (like when we add a title to a movie poster and scroll to it), and sure, one may directly use TVPosterView from TVUIKit(like in this example) if that is the goal, but it works on single labels nonetheless.
只有当祖先被聚焦时才能获得选框效果(例如当我们向电影海报添加标题并滚动到它时),当然,如果这是目标,可以直接使用 TVUIKit 中的TVPosterView(如本例中),但它仍然适用于单个标签。
回答by user11027368
In Swift 4
在斯威夫特 4
CODE
代码
UIView.animate(withDuration: 1.0, delay: 1, options: ([.curveLinear, .repeat]), animations: {() -> Void in
self.marqueeLABEL.center = CGPoint(x: self.marqueeLABEL.bounds.size.width, y: self.marqueeLABEL.center.y)
}, completion: { _ in })