0

I've written some code that moves some objects around on the screen in sequence. As each object finishes moving, the next starts.

The code is structured similarly to this:

Model:

moveObject
{
    // Code to move the object
    ...

    [delegate moved:self];
}

Delegate (Controller):

moved:(MyClass *)o
{
    UIView* v = [self viewForObject:o];

    [UIView animateWithDuration:1.0
                     animations:^{
                         [v setCenter: [model center]];
                     }
                     completion:^(BOOL finished){
                         // Move object
                         [model moveObject];
                     }];
}

Aside from the fact that this doesn't actually work correctly, it doesn't seem conceptually right to me. The logic in the model should be free to carry on moving things around without having to wait to be nudged by the controller telling it that the views are ready for more updates.

My plan is to create a queue of pending actions-requiring-animation, and each time the model updates something which needs animating, just add it to the queue and let it carry on. Then I could pluck actions off the queue and animate them one at a time.

This would also mean that I could use the model without any views at all, without having to write a separate controller that just keeps calling back into the model when objects are moved.

So before I start working on my own solution for this, is there already code to handle this in Apple's APIs? It seems like it would be a common enough problem that there would be a standard method of handling it.

Alternatively, is there a better way to handle building chains of animations at run-time? (As opposed to simply hard coding them using the completion block.)

Community
  • 1
  • 1
Rich
  • 7,348
  • 4
  • 34
  • 54
  • My impression is that your view controller should take care of this. It should know which properties need to be animated and how the animation should look. Perhaps use KVO to observe the relevant model changes. Regarding the queue, I have no comment :) – Carl Veazey Sep 04 '12 at 18:31
  • 1
    You could look at mine and Duncan's answers to [this question](http://stackoverflow.com/q/11737658/608157) They are two different ways of chaining animations, making them sequential. Both are good answers. – David Rönnqvist Sep 05 '12 at 06:22
  • @DavidRönnqvist Thanks, your answer there is the sort of thing I'm planning to do. I'll give it a shot. – Rich Sep 05 '12 at 06:59

0 Answers0