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?

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Mar 22, 2014 at 13:36
\$\endgroup\$
4
  • \$\begingroup\$ animateWithCoreAnimaGenerallyWithIndex:? Find & replace error or your own method? \$\endgroup\$ Commented Mar 22, 2014 at 16:42
  • \$\begingroup\$ @Flambino, Hi thanks, didn't understand your question, I got no error, was this the question? \$\endgroup\$ Commented Mar 22, 2014 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\$ Commented Mar 22, 2014 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\$ Commented Mar 23, 2014 at 0:38

1 Answer 1

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).

answered Mar 23, 2014 at 1:20
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.