Skip to content

Commit

Permalink
Release 3.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rwbutler committed Jan 2, 2021
1 parent ed9e0ca commit 634f0f1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion AnimatedGradientView.podspec
Original file line number Diff line number Diff line change
@@ -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
Expand Down
38 changes: 34 additions & 4 deletions AnimatedGradientView/Classes/AnimatedGradientView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 634f0f1

Please sign in to comment.