objective c, animation block, delay between animation and completion

11,371

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;
}
Share:
11,371
lunadiviner
Author by

lunadiviner

Updated on June 04, 2022

Comments

  • lunadiviner
    lunadiviner almost 2 years

    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.

  • lunadiviner
    lunadiviner over 12 years
    it's throwing an EXC_BAD_ACCESS when it tries to start running the completion animation. switched the numbers to what I need them to be and it didn't fix it, not sure why it's happening because the code looks fine
  • Paul.s
    Paul.s over 12 years
    Can you show the code you used and where the EXC_BAD_ACCESS is happening. Apart from the two calls to your animations the code I posted was from a working example.