lunadiviner
lunadiviner

Reputation: 509

objective c, animation block, delay between animation and completion

I have several animation blocks, all of which follow this basic format with different delays so that they fire one after another:

[UIView animateWithDuration:.85 delay:3 options:opts animations:[animations objectAtIndex:ww] completion:[completions objectAtIndex:ww]];

The options are just UIViewAnimationOptionAutoreverse in a variable for easy access.

I want there to be a delay between the animations and completion so that the images stay in their new position for a little bit before returning to the original one. I've considered using several simpler animateWithDuration:animations: blocks but I didn't see any way to do that with the delay in the documentation, unless I'm missing something.

@Paul.s here's the code I used with what you gave me:

void (^completion)(void) = ^{
[UIView animateWithDuration:.5
                  delay:5
                options:UIViewAnimationCurveLinear
             animations:[completions objectAtIndex:ww]
             completion:^(BOOL finished) {}];
};


// Call your existing animation with the new completion block
[UIView animateWithDuration:.5
                  delay:1
                options:UIViewAnimationCurveLinear
             animations:[animations objectAtIndex:ww]
             completion:^(BOOL finished) {
               completion();
             }];

for reference, the animation is super simple, just moving an image from one point to another and then back. the point at which it crashes is the [UIView animateWithDuration:.5 line where the completion block is defined, and it crashes after the first part of the animation runs.

Upvotes: 3

Views: 11648

Answers (1)

Paul.s
Paul.s

Reputation: 38728

How about passing another animation to the completion?

Updated I have updated the code to be the exact working code from the sample I set up. This is using a clean project set up using the Empty Application template

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    CGRect startFrame = CGRectMake(0, 0, 100, 100);

    UIView *view = [[UIView alloc] initWithFrame:startFrame];
    view.backgroundColor = [UIColor greenColor];

    [self.window addSubview:view];

    // Set up your completion animation in a block
    void (^completion)(void) = ^{
        [UIView animateWithDuration:0.5f
                              delay:0.5f
                            options:UIViewAnimationCurveLinear
                         animations:^{
                             view.frame = startFrame;
                         }
                         completion:nil];
      };


      // Call your existing animation with the new completion block
      [UIView animateWithDuration:4
                            delay:1
                          options:UIViewAnimationCurveLinear
                       animations:^{
                           view.frame = CGRectMake(200, 200, 10, 10);
                       }
                       completion:^(BOOL finished) {
                           completion();
                       }];



        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        return YES;
}

Upvotes: 6

Related Questions