-
-
Notifications
You must be signed in to change notification settings - Fork 565
/
SingleShotOffscreenLabel.swift
38 lines (34 loc) · 1.35 KB
/
SingleShotOffscreenLabel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//
// SingleShotOffscreenLabel.swift
// MarqueeLabelSwift
//
// Created by Charles Powell on 10/24/16.
// Copyright © 2016 Charles Powell. All rights reserved.
//
import UIKit
open class SingleShotOffscreenLabel: MarqueeLabel {
// Override layoutSubviews to catch frame size changes
open override func layoutSubviews() {
// Set trailingBuffer to frame width + 10 (to be sure)
trailingBuffer = frame.width
super.layoutSubviews()
}
// Override labelWillBeginScroll to catch when a scroll animation starts
open override func labelWillBeginScroll() {
// This only makes sense for leftRight and rightLeft types
if type == .leftRight || type == .rightLeft {
// Calculate "away" position time after scroll start
let awayTime = animationDelay + animationDuration
// Schedule a timer to restart the label when it hits the "away" position
Timer.scheduledTimer(timeInterval: TimeInterval(awayTime), target: self, selector: #selector(pauseAndClear), userInfo: nil, repeats: false)
}
}
@objc public func pauseAndClear() {
// Pause label offscreen
pauseLabel()
// Change text to nil
text = nil
// Unpause (empty) label, clearing the way for a subsequent text change to trigger scroll
unpauseLabel()
}
}