UIView.animate() within a loop

I have an outlet collection. I want to animate changing their properties. Is it reasonable to put a animation within a loop?

for outlet in outletCollection {
  UIView.animate(withDuration: 0.5, ....
          animations: {
               outlet.backgroundColor = ...
               }
   }

@u3qu3oxyvflx Thanks very much for your question!

Best way to find out is to try and see what happens! :slight_smile:

This is precisely how as a young developer you gain confidence…by being creative, using your imagination with the tools you’ve been given, and pushing the boundaries and see what happens!

Why not try it out, and share your experiences with the rest of us? Don’t be afraid to make mistakes. We all go through it, and it’s part of the process of becoming better :slight_smile:

I hope this helps!

All the best!

Well, it worked and the app didn’t crash, but I still don’t know if it’s a reasonable idea. After all, I could write a tableView(… cellForRowAt,) and not use a dequeueCelll. It works in a small app and won’t crash, but it’s not the right thing to do.

hi @u3qu3oxyvflx,
I appreciate your initial question, let’s try to look at it in two parts.

  1. Will It Crash?
    You found that it does not. After all if you are animating, the possibility is that it shouldn’t. However if at the end of the animation, you remove some element or constraint that could be a dependency for another item, then you can potentially end up with a crash.

  2. Is this a Good Idea?
    The short answer is it depends.

it all depends on the effect that you are trying to achieve, you can also queue animations to happen one after the other like you would in powerpoint/keynote or all at once in parallel.

I think asking the question before you started is what will make you a better programmer, trying everything will not answer some of the other questions like efficiency etc. A kamikaze approach can get a problem solved but more often isn’t the most efficient method.

on a side note, consider parsing each character in a string and converting it vs using a function like compactMap both will resolve the problem quite fast in playgrounds. Have a look at the ‘Adopting Algorithms’ WWDC video where you will find something similar, the problem can increase exponetially.

cheers,

Jayant

This topic was automatically closed after 166 days. New replies are no longer allowed.