From 5b850913c6534a5e7bb8c52ac389c4d8d981d4ac Mon Sep 17 00:00:00 2001 From: Charles Powell Date: Mon, 24 Oct 2016 17:31:38 -0700 Subject: [PATCH] Add "Extras" (subclass, categories, etc) to repo. [common] --- Extras/NoReturnAnimation.swift | 21 ++++++++++ Extras/SingleShotOffscreenLabel.swift | 38 +++++++++++++++++++ .../contents.xcworkspacedata | 3 ++ .../project.pbxproj | 17 +++++++++ 4 files changed, 79 insertions(+) create mode 100644 Extras/NoReturnAnimation.swift create mode 100644 Extras/SingleShotOffscreenLabel.swift diff --git a/Extras/NoReturnAnimation.swift b/Extras/NoReturnAnimation.swift new file mode 100644 index 00000000..01958bf3 --- /dev/null +++ b/Extras/NoReturnAnimation.swift @@ -0,0 +1,21 @@ +// +// NoAnimationReturn.swift +// MarqueeLabel +// +// Created by Charles Powell on 10/23/16. +// Copyright © 2016 Charles Powell. All rights reserved. + +import UIKit + +open class NoAnimationReturn: MarqueeLabel { + // Override labelWillBeginScroll to catch when a scroll animation starts + override open 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(restartLabel), userInfo: nil, repeats: false) + } + } +} diff --git a/Extras/SingleShotOffscreenLabel.swift b/Extras/SingleShotOffscreenLabel.swift new file mode 100644 index 00000000..4b765bbc --- /dev/null +++ b/Extras/SingleShotOffscreenLabel.swift @@ -0,0 +1,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) + } + } + + 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() + } +} diff --git a/MarqueeLabel.xcworkspace/contents.xcworkspacedata b/MarqueeLabel.xcworkspace/contents.xcworkspacedata index 2facbae4..aebb7c70 100644 --- a/MarqueeLabel.xcworkspace/contents.xcworkspacedata +++ b/MarqueeLabel.xcworkspace/contents.xcworkspacedata @@ -4,6 +4,9 @@ + + diff --git a/MarqueeLabelSwift/MarqueeLabelSwift.xcodeproj/project.pbxproj b/MarqueeLabelSwift/MarqueeLabelSwift.xcodeproj/project.pbxproj index 617bb131..8ccd85d8 100644 --- a/MarqueeLabelSwift/MarqueeLabelSwift.xcodeproj/project.pbxproj +++ b/MarqueeLabelSwift/MarqueeLabelSwift.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + EA017CE81DBED75E00237CA9 /* SingleShotOffscreenLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA017CE71DBED75E00237CA9 /* SingleShotOffscreenLabel.swift */; }; + EA017CEA1DBEDCD900237CA9 /* NoReturnAnimation.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA017CE91DBEDCD900237CA9 /* NoReturnAnimation.swift */; }; EAE4AC591CA8DAC0006C1ECC /* MLHeader.xib in Resources */ = {isa = PBXBuildFile; fileRef = EAE4AC581CA8DAC0006C1ECC /* MLHeader.xib */; }; EAE4AC5B1CA8DAC8006C1ECC /* MarqueeLabel.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = EAE4AC5A1CA8DAC8006C1ECC /* MarqueeLabel.storyboard */; }; EAE4AC5F1CA8DAFB006C1ECC /* MarqueeLabelDemoPushController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAE4AC5C1CA8DAFB006C1ECC /* MarqueeLabelDemoPushController.swift */; }; @@ -20,6 +22,8 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + EA017CE71DBED75E00237CA9 /* SingleShotOffscreenLabel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SingleShotOffscreenLabel.swift; path = ../../Extras/SingleShotOffscreenLabel.swift; sourceTree = ""; }; + EA017CE91DBEDCD900237CA9 /* NoReturnAnimation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NoReturnAnimation.swift; path = ../../Extras/NoReturnAnimation.swift; sourceTree = ""; }; EA167D021CA8B43700DBF930 /* MarqueeLabelSwiftDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MarqueeLabelSwiftDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; EA167D111CA8B43700DBF930 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; EAE4AC581CA8DAC0006C1ECC /* MLHeader.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = MLHeader.xib; sourceTree = SOURCE_ROOT; }; @@ -53,12 +57,23 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + EA017CE61DBED65E00237CA9 /* Extras */ = { + isa = PBXGroup; + children = ( + EA017CE91DBEDCD900237CA9 /* NoReturnAnimation.swift */, + EA017CE71DBED75E00237CA9 /* SingleShotOffscreenLabel.swift */, + ); + name = Extras; + path = MarqueeLabelSwift; + sourceTree = ""; + }; EA167CF91CA8B43700DBF930 = { isa = PBXGroup; children = ( EA167D041CA8B43700DBF930 /* MarqueeLabelSwift Demo */, EAE4AC801CA8DE70006C1ECC /* MarqueeLabelSwift Framework */, EA167D031CA8B43700DBF930 /* Products */, + EA017CE61DBED65E00237CA9 /* Extras */, ); sourceTree = ""; }; @@ -248,6 +263,8 @@ EAE4ACCF1CAA2495006C1ECC /* MarqueeLabel.swift in Sources */, EAE4AC661CA8DBBA006C1ECC /* AppDelegate.swift in Sources */, EAE4AC611CA8DAFB006C1ECC /* MarqueeLabelTableViewController.swift in Sources */, + EA017CE81DBED75E00237CA9 /* SingleShotOffscreenLabel.swift in Sources */, + EA017CEA1DBEDCD900237CA9 /* NoReturnAnimation.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };