|
Greetings, I have an image that I animate when an action is being performed. And it works well if the animated section of the view stays in sight. But when I scroll the page down to the bottom and go back up, the animation stopped. You can see a demo here: https://imgur.com/a/pEKEJgj Hosts www.apple.com and www.bing.com start the animation properly, but when I scroll down and back up, it stopped. This is the code for the animation itself:
And this is the code to call the animation in the view:
I've also tried using a dedicated Swift file as a View like this:
But got the same result. Anyone else faced the same issue and found a solution for it? Thank you :-) |
|
This response may not be 100% accurate. But it's what I remember from other animation lectures. Imagine you have a simple square in your layout with a variable for the background color, currently
You have marked the square shape with an animation sequence so that anytime you change the background color, it will slowly change to the new color over five seconds. Simple animation. So what happens when you actually change the variable to What happens is the variable is changed instantly! It was But remember, the background color variable is already set to Now consider your case. While on screen, the animation is taking five seconds to run its course. But then you scroll the So it looks at the Ask your self this: if you were to recreate the view, what parameters do you need? To color the square, you need to know its background color. It's When recreating the |
|
So are you saying it's not possible to keep an animation going when you scroll a list up and down? Or are you saying I'm not using the appropriate way to do it? |
|
I wrote a short test. Turns out (predictably) that I was wrong. Paste this code into a new project and run in a simulator. Tap the Animate toggle to start the animation. Notice how the values of the foreground color for each row of the list change immediately. However the fade animations take 8 seconds to complete. This gives you time to scroll the list so the first few rows go OFF the screen. Count to three. Then release your scroll. The first few rows return to the screen, but are still animating.
|
|
Hi Obelix, I tried you code, and you might have missed it, but when you scroll, you can see the last line, 2nd occurence of purple, is already purple when you reach the bottom, as it's not on screen when it's loaded. I've upped the time to complete and still purple is instantly purple while the other ones are fading slowly. The issue with your example is that there are not enough lines. I've added 2 additional groups, to make it 40 lines, and this is more obvious this way. Up the time to 60 seconds, and run the code. The lines at the bottom are already fully colored, and when you go back to the top, the same applies to the ones that were fading slowly before scrolling. Either it's not supposed to work at all or it needs to be written in another way or it's a bug, I'm not sure at this point. |
BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.