Я использую модуль из github под названием Pastel https://github.com/cruisediary/Pastel. Он обеспечивает легкую градиентную цветную анимацию для фона.Моя проблема в том, что анимированный градиент останавливается при переходе к другому UIView и возвращению к оригиналу.Любая идея о том, как запустить и остановить эту анимацию при переходе на другие вкладки или представления?Это основная часть файла pod `import UIKit
, открытый класс PastelView: UIView {
private struct Animation {
static let keyPath = "colors"
static let key = "ColorChange"
}
//MARK: - Custom Direction
open var startPoint: CGPoint = PastelPoint.topRight.point
open var endPoint: CGPoint = PastelPoint.bottomLeft.point
open var startPastelPoint = PastelPoint.topRight {
didSet {
startPoint = startPastelPoint.point
}
}
open var endPastelPoint = PastelPoint.bottomLeft {
didSet {
endPoint = endPastelPoint.point
}
}
//MARK: - Custom Duration
open var animationDuration: TimeInterval = 5.0
fileprivate let gradient = CAGradientLayer()
private var currentGradient: Int = 0
private var colors: [UIColor] = [UIColor(red: 156/255, green: 39/255, blue: 176/255, alpha: 1.0),
UIColor(red: 255/255, green: 64/255, blue: 129/255, alpha: 1.0),
UIColor(red: 123/255, green: 31/255, blue: 162/255, alpha: 1.0),
UIColor(red: 32/255, green: 76/255, blue: 255/255, alpha: 1.0),
UIColor(red: 32/255, green: 158/255, blue: 255/255, alpha: 1.0),
UIColor(red: 90/255, green: 120/255, blue: 127/255, alpha: 1.0),
UIColor(red: 58/255, green: 255/255, blue: 217/255, alpha: 1.0)]
public override init(frame: CGRect) {
super.init(frame: frame)
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
open override func awakeFromNib() {
super.awakeFromNib()
}
open override func layoutSubviews() {
super.layoutSubviews()
gradient.frame = bounds
}
public func startAnimation() {
gradient.removeAllAnimations()
setup()
animateGradient()
}
fileprivate func setup() {
gradient.frame = bounds
gradient.colors = currentGradientSet()
gradient.startPoint = startPoint
gradient.endPoint = endPoint
gradient.drawsAsynchronously = true
layer.insertSublayer(gradient, at: 0)
}
fileprivate func currentGradientSet() -> [CGColor] {
guard colors.count > 0 else { return [] }
return [colors[currentGradient % colors.count].cgColor,
colors[(currentGradient + 1) % colors.count].cgColor]
}
public func setColors(_ colors: [UIColor]) {
guard colors.count > 0 else { return }
self.colors = colors
}
public func setPastelGradient(_ gradient: PastelGradient) {
setColors(gradient.colors())
}
public func addcolor(_ color: UIColor) {
self.colors.append(color)
}
func animateGradient() {
currentGradient += 1
let animation = CABasicAnimation(keyPath: Animation.keyPath)
animation.duration = animationDuration
animation.toValue = currentGradientSet()
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false
animation.delegate = self
animation.autoreverses = true
animation.repeatCount = Float.infinity
gradient.add(animation, forKey: Animation.key)
}
open override func removeFromSuperview() {
super.removeFromSuperview()
gradient.removeAllAnimations()
gradient.removeFromSuperlayer()
расширение PastelView: CAAnimationDelegate {
public func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
if flag {
gradient.colors = currentGradientSet()
animateGradient()
}
}
и отображается вКонтроллер вида как
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Moving Gradient View
let pastelView = PastelView(frame: view.bounds)
//MARK: - Custom Direction
pastelView.startPastelPoint = .bottomRight
pastelView.endPastelPoint = .topLeft
//MARK: - Custom Duration
pastelView.animationDuration = 1.60
//MARK: - Custom Color
pastelView.setColors([UIColor(red: 255/255, green: 30/255, blue: 140/255, alpha: 1.0),
UIColor(red: 255/255, green: 48/255, blue: 129/255, alpha: 1.0),
UIColor(red: 254/255, green: 66/255, blue: 121/255, alpha: 1.0),
UIColor(red: 253/255, green: 91/255, blue: 114/255, alpha: 1.0),
UIColor(red: 253/255, green: 105/255, blue: 109/255, alpha: 1.0),
])
pastelView.startAnimation()
view.insertSubview(pastelView, at: 0)
}