Search code examples
swiftswift3uilabelmarquee

Swift 3 Marquee Label Notification Before Repeats or At Home Position


I am using Marquee label in Swift 3, its working fine. The only thing is I am having trouble with the following:

I am trying to find out when the label returns to the home position. When the text returns home it is off screen, this is when I would like to update the dataString.

If I try updating the string during the scroll it automatically stops and resets. Also the dataString length is variable so i can't use timers or anything like that.

I have had a read through everything here Marquee Label

This is my code that I'm using to setup and start my label:

feedLabel.text = dataString
    feedLabel.type = .continuous
    feedLabel.speed = .rate(70)
    feedLabel.fadeLength = 80.0
    feedLabel.leadingBuffer = 1300.0
    feedLabel.trailingBuffer = 1300.0
    feedLabel.labelWillBeginScroll()

My dataString updates every few seconds but i only want my label to update just before it repeats.

This has been blowing a hole in my brain. Any suggestions would be greatly appreciated.


Solution

  • You can create new inherited class from MarqueeLabel and override labelReturnedToHome function:

    class CustomMarqueLabel : MarqueeLabel {
        open var returned : ((Bool)->Void)?
        override func labelReturnedToHome(_ finished: Bool) {
            super.labelReturnedToHome(finished)
            if returned != nil {
                returned!(finished)
            }
        }
    }
    

    Then use like this:

    let lengthyLabel = CustomMarqueLabel(frame: CGRect(x: 20, y: 40, width: 200, height: 20), duration: 0.5, fadeLength: 10.0)
    lengthyLabel.returned = {completed in
                //label returned to begining
            }