Coder Social home page Coder Social logo

Comments (5)

tapizquent avatar tapizquent commented on May 30, 2024 2

NVM! I fixed it. I just added a var to your PastelView class to check whether is currently animating or not. and if its not, then I startAnimation() inside viewDidAppear.

open class PastelView: UIView {

    public var isAnimating: Bool = false   // This right here!

    private struct Animation {
        static let keyPath = "colors"
        static let key = "ColorChange"
    }

And then in

public func startAnimation() {
    self.isAnimating = true   // Here
    gradient.removeAllAnimations()
    setup()
    animateGradient()
}

extension PastelView: CAAnimationDelegate {
    public func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        if flag {
             gradient.colors = currentGradientSet()
             animateGradient()
        } else {
            self.isAnimating = false
        }
     }
}

And lastly

override func viewDidLoad() {
    pastelView.startAnimation()
}
override func viewDidAppear(_ animated: Bool) {
    if !pastelView.isAnimating {
        pastelView.startAnimation()
    }
}

from pastel.

Rida-ba avatar Rida-ba commented on May 30, 2024 2

Thanks it help me to figure it out.

I did like this in order to keep animation working (Using notification observer):

override func viewDidAppear(_ animated: Bool) {

    super.viewDidAppear(animated)
    /// Notification foreground observer
    NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name:          UIApplication.willEnterForegroundNotification, object: nil)
}

Then call this :

/// MARK: -  Custom Func
@objc func willEnterForeground() {
    /// Play animation
    if !pastelView.isAnimating {
        print("Play Animation")
        pastelView.startAnimation()
    }
}

In my case it works when my app will enter in foreground and will appear.
But you can use the same method with another view controller. (Using Notification and call objc func in another VC)

And don't forget to remove the observer :

NotificationCenter.default.removeObserver(self, name: UIApplication.willEnterForegroundNotification, object: nil)

I hope it works for you.

Happy New Year !!!!

from pastel.

tapizquent avatar tapizquent commented on May 30, 2024

NVM still opened because animation stops midway. No idea why

from pastel.

ucelme avatar ucelme commented on May 30, 2024

Can you provide the full code to fix animations? I get Use of unresolved identifier 'pastelView'

from pastel.

DorukhanArslan avatar DorukhanArslan commented on May 30, 2024

There is no need to hold an extra property (isAnimating) in base class. This will handle all cases:

final class AnimatedGradientViewController: UIViewController {

    private lazy var animatedGradientView: PastelView = {
        ...
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        // 1. Starts animation when view controller is loaded initially.
        animatedGradientView.startAnimation()

        // Insert animated gradient view as subview once.
        view.insertSubview(animatedGradientView, at: 0)

        // Add 'willEnterForeground' notification observer once. First launch does not count.
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(willEnterForeground),
            name: UIApplication.willEnterForegroundNotification,
            object: nil)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        // View is not appeared at the end of neither dismiss or pop, it means view is seen after
        // dismiss or pop of a view controller.
        guard !isBeingPresented && !isMovingToParent else {
            return
        }

        // 2. Starts animation if view controller is seen after dismiss or pop.
        animatedGradientView.startAnimation()
    }

    @objc
    private func willEnterForeground() {
        // 3. Starts animation if app was entered background, e.g. locked or gone to home screen.
        animatedGradientView.startAnimation()
    }
}

from pastel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.