How to use MarqueeLabel in Swift?

Swift picture Swift · Feb 3, 2016 · Viewed 15.3k times · Source

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

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).

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.

My questions are:

  1. Where did I go wrong with the code?

  2. Is there an alternative way to display marquee effect using Swift?

Thanks in advance.

Answer

Swift picture Swift · Mar 3, 2016

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.