2
\$\begingroup\$

I have some animations happening after some delay calls the code to animate. It works fine, but the code to call the functions is not nice.

    double delayInSeconds = 0.2;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds + 0.1 * NSEC_PER_SEC);
    dispatch_time_t popTime2 = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds + 0.2 * NSEC_PER_SEC);
    dispatch_time_t popTime3 = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds + 0.3 * NSEC_PER_SEC);
    dispatch_time_t popTime4 = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds + 0.4 * NSEC_PER_SEC);
    dispatch_time_t popTime5 = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds + 0.5 * NSEC_PER_SEC);
    dispatch_time_t popTime6 = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds + 0.6 * NSEC_PER_SEC);
    dispatch_time_t popTime7 = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds + 0.7 * NSEC_PER_SEC);
    dispatch_time_t popTime8 = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds + 0.8 * NSEC_PER_SEC);

    if (self.menuType == kRacingMenu) {

        self.menuAnimationSequence = @[@0, @1, @3, @2, @4]; //animation sequence!

        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [self animateWithCoreAnimaGenerallyWithIndex:0];
        });
        dispatch_after(popTime2, dispatch_get_main_queue(), ^(void){
        [self animateWithCoreAnimaGenerallyWithIndex:1];
        });
        dispatch_after(popTime3, dispatch_get_main_queue(), ^(void){
        [self animateWithCoreAnimaGenerallyWithIndex:2];
        });
        dispatch_after(popTime4, dispatch_get_main_queue(), ^(void){
        [self animateWithCoreAnimaGenerallyWithIndex:4];
        });
        dispatch_after(popTime5, dispatch_get_main_queue(), ^(void){
        [self animateWithCoreAnimaGenerallyWithIndex:3];
        });


    }else if (self.menuType == kLoginMenu) {
        self.menuAnimationSequence = @[@0, @1, @3, @2]; //animation sequence!
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [self animateWithCoreAnimaGenerallyWithIndex:0];
        });
        dispatch_after(popTime2, dispatch_get_main_queue(), ^(void){
            [self animateWithCoreAnimaGenerallyWithIndex:1];
        });
        dispatch_after(popTime3, dispatch_get_main_queue(), ^(void){
            [self animateWithCoreAnimaGenerallyWithIndex:2];
        });
        dispatch_after(popTime4, dispatch_get_main_queue(), ^(void){
            [self animateWithCoreAnimaGenerallyWithIndex:3];
        });


    } else {
        [self animateWithCoreAnimaGenerallyWithIndex:1];
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [self animateWithCoreAnimaGenerallyWithIndex:0];
        });
        dispatch_after(popTime2, dispatch_get_main_queue(), ^(void){
            [self animateWithCoreAnimaGenerallyWithIndex:2];
        });
        dispatch_after(popTime3, dispatch_get_main_queue(), ^(void){
            [self animateWithCoreAnimaGenerallyWithIndex:4];
        });
        dispatch_after(popTime4, dispatch_get_main_queue(), ^(void){
            [self animateWithCoreAnimaGenerallyWithIndex:3];
        });
        dispatch_after(popTime5, dispatch_get_main_queue(), ^(void){
            [self animateWithCoreAnimaGenerallyWithIndex:5];
        });
        dispatch_after(popTime6, dispatch_get_main_queue(), ^(void){
            [self animateWithCoreAnimaGenerallyWithIndex:7];
        });
        dispatch_after(popTime7, dispatch_get_main_queue(), ^(void){
            [self animateWithCoreAnimaGenerallyWithIndex:6];
        });
        dispatch_after(popTime8, dispatch_get_main_queue(), ^(void){
            [self animateWithCoreAnimaGenerallyWithIndex:8];
        });

    }

How best to abstract this timer? And the calls to start the animation?

\$\endgroup\$
  • \$\begingroup\$ animateWithCoreAnimaGenerallyWithIndex:? Find & replace error or your own method? \$\endgroup\$ – Flambino Mar 22 '14 at 16:42
  • \$\begingroup\$ @Flambino, Hi thanks, didn't understand your question, I got no error, was this the question? \$\endgroup\$ – manuelBetancurt Mar 22 '14 at 16:49
  • \$\begingroup\$ It looks like you can accomplish a lot of this with a for loop and an incremented delay variable passed to a UIView animation block. \$\endgroup\$ – JeffRegan Mar 22 '14 at 18:38
  • \$\begingroup\$ What I meant was that animateWithCoreAnimaGenerallyWithIndex: doesn't sound like a real method name. I mean, "AnimaGenerally"? It just seemed like it might be a mistake. But if you have a method that's called that, then OK - it's just a really strange method name. \$\endgroup\$ – Flambino Mar 23 '14 at 0:38
3
\$\begingroup\$

Anything that you're using as a constant should be marked as such.

double const delayInSeconds = 0.2f;

Now then, we know in Objective-C, we have NSArray and its mutable cousin, and they can only hold objects. But also remember, Objective-C is a superset of C. We can use a C-Style array.

int const POPTIME_COUNT = 8;

dispatch_time_t poptime[POPTIME_COUNT];

And then fill this an array with a loop...

for (int i = 0; i < POPTIME_COUNT; ++i) {
    double totalDelay = delayInSeconds + ((i + 1) * 0.1 * NSEC_PER_SEC)                
    poptime[i] = dispatch_time(DISPATCH_TIME_NOW, totalDelay);
}

Firing off the animations is done just as easily in a loop:

for (int i = 0; i < NUM_ANIMATIONS; ++i) {
    dispatch_after(poptime[i], dispatch_get_main_queue()), ^(void){
        [self animateWithCoreAnimaGenerallyWithIndex:i];
    }];
}

Where NUM_ANIMATIONS is the number of animations you need to fire off (it's different in each branch).

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.