Comments (5)
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.
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.
NVM still opened because animation stops midway. No idea why
from pastel.
Can you provide the full code to fix animations? I get Use of unresolved identifier 'pastelView'
from pastel.
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)
- Animation stops when returns to view using Navigation Bar HOT 7
- Unable to find a specification for `Pastel` HOT 1
- Question: How did you do to manage the updating bacground without concurrency issues... HOT 1
- Question: memory seems to be leaked HOT 5
- pastel view remain below the status bar
- Lag in collection views
- Swift 4 Version HOT 5
- Is there a way to continue the gradient animation on a different ViewController? HOT 3
- Memory leaked CG colors HOT 1
- QUESTION: How to put the animation on a continuous loop. HOT 2
- Swift 4.2 Support HOT 3
- Restore state after application enters background. HOT 3
- Animation just work once on other view controllers.
- Can't reset gradient view?
- Implement PastelLayer HOT 1
- Carthage build failed HOT 1
- it's awesome,how can i use it in flutter/dart
- Can't remove "Using low GPU priority for background rendering."
- WORKAROUND: Animation stops when another view controller is presented
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pastel.