From 634f0f1399a536abe5cc30762f0d015d72e6b07e Mon Sep 17 00:00:00 2001 From: Ross Butler Date: Sat, 2 Jan 2021 23:28:32 +0000 Subject: [PATCH] Release 3.1.0 --- AnimatedGradientView.podspec | 2 +- .../Classes/AnimatedGradientView.swift | 38 +++++++++++++++++-- CHANGELOG.md | 5 +++ Example/Pods/Pods.xcodeproj/project.pbxproj | 4 +- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/AnimatedGradientView.podspec b/AnimatedGradientView.podspec index 76bd41f..e405889 100644 --- a/AnimatedGradientView.podspec +++ b/AnimatedGradientView.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'AnimatedGradientView' - s.version = '3.0.0' + s.version = '3.1.0' s.swift_version = '5.0' s.summary = 'Powerful gradient animations made simple for iOS.' s.description = <<-DESC diff --git a/AnimatedGradientView/Classes/AnimatedGradientView.swift b/AnimatedGradientView/Classes/AnimatedGradientView.swift index af69d6b..b674738 100644 --- a/AnimatedGradientView/Classes/AnimatedGradientView.swift +++ b/AnimatedGradientView/Classes/AnimatedGradientView.swift @@ -104,6 +104,9 @@ public class AnimatedGradientView: UIView { private var gradientColorIndex: Int = 0 + /// Internal state variable prevents the next frame from animating. + private var isPaused = false + private var longestColorArrayCount: Int { if let gradientAnimations = animations { return gradientAnimations.reduce(0) { (total, animation) in @@ -128,10 +131,12 @@ public class AnimatedGradientView: UIView { // MARK: - View Life Cycle public override init(frame: CGRect) { super.init(frame: frame) + observeApplicationDidBecomeActive() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) + observeApplicationDidBecomeActive() } override public func layoutSubviews() { @@ -151,6 +156,17 @@ public class AnimatedGradientView: UIView { } public extension AnimatedGradientView { + func pauseAnimating() { + isPaused = true + } + + func resumeAnimating() { + isPaused = false + if let gradient = self.gradient { + animate(gradient, to: gradientNextColors) + } + } + func startAnimating() { if gradient == nil { gradient = configuredGradientLayer() @@ -168,9 +184,7 @@ public extension AnimatedGradientView { } private extension AnimatedGradientView { - func animate(_ gradient: CAGradientLayer?, to colors: [CGColor]) { - let locationsAnimation = CABasicAnimation(keyPath: #keyPath(CAGradientLayer.locations)) locationsAnimation.fromValue = gradient?.locations locationsAnimation.toValue = locations(for: colors) @@ -203,6 +217,12 @@ private extension AnimatedGradientView { gradient.add(animationGroup, forKey: "gradient-color-\(gradientColorIndex)") } + @objc func applicationDidBecomeActive(_: NSNotification) { + if autoAnimate { + resumeAnimating() + } + } + func locations(for colors: [CGColor]) -> [NSNumber] { let uniqueColors = colors.uniqueMap({ $0 }) let colorsCountDiff = colors.count - uniqueColors.count @@ -275,6 +295,16 @@ private extension AnimatedGradientView { return shape } + private func observeApplicationDidBecomeActive() { + let notificationCenter = NotificationCenter.default + notificationCenter.addObserver( + self, + selector: #selector(applicationDidBecomeActive(_:)), + name: UIApplication.didBecomeActiveNotification, + object: nil + ) + } + private func path(from startPoint: CGPoint, to stopPoint: CGPoint) -> CGPath { let path = UIBezierPath() path.move(to: startPoint) @@ -285,8 +315,8 @@ private extension AnimatedGradientView { extension AnimatedGradientView: CAAnimationDelegate { public func animationDidStop(_ anim: CAAnimation, finished flag: Bool) { - let shouldRepeat = ((gradientColorIndex % (animationsCount + 1)) == 0 && autoRepeat) - || (gradientColorIndex % (animationsCount + 1)) != 0 + let isLastAnimation = (gradientColorIndex % (animationsCount + 1)) == 0 + let shouldRepeat = ((isLastAnimation && autoRepeat) || !isLastAnimation) && !isPaused if flag, shouldRepeat, let gradient = self.gradient { if currentGradientType != gradient.type { type = currentGradientType diff --git a/CHANGELOG.md b/CHANGELOG.md index ccea65f..88f594d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [3.1.0] - 2021-01-02 +### Added +- Support for pausing and resuming animation. +- Support for auto-resuming animation on application becoming active. + ## [3.0.0] - 2020-09-22 ### Added - Support for Xcode 12. diff --git a/Example/Pods/Pods.xcodeproj/project.pbxproj b/Example/Pods/Pods.xcodeproj/project.pbxproj index 50c25ca..0c140c7 100644 --- a/Example/Pods/Pods.xcodeproj/project.pbxproj +++ b/Example/Pods/Pods.xcodeproj/project.pbxproj @@ -472,7 +472,7 @@ INFOPLIST_FILE = "Target Support Files/AnimatedGradientView/Info.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; - MARKETING_VERSION = 3.0.0; + MARKETING_VERSION = 3.1.0; MODULEMAP_FILE = "Target Support Files/AnimatedGradientView/AnimatedGradientView.modulemap"; PRODUCT_BUNDLE_IDENTIFIER = com.rwbutler.AnimatedGradientView; PRODUCT_MODULE_NAME = AnimatedGradientView; @@ -730,7 +730,7 @@ INFOPLIST_FILE = "Target Support Files/AnimatedGradientView/Info.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; - MARKETING_VERSION = 3.0.0; + MARKETING_VERSION = 3.1.0; MODULEMAP_FILE = "Target Support Files/AnimatedGradientView/AnimatedGradientView.modulemap"; PRODUCT_BUNDLE_IDENTIFIER = com.rwbutler.AnimatedGradientView; PRODUCT_MODULE_NAME = AnimatedGradientView;