0

I want to build in a layer and build out it after 5 seconds. but I don't know how to assign the final value to the layer. my code is as following:

-(CABasicAnimation *) buildIn{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    animation.duration = 2.0f;
    [animation setFromValue:[NSNumber numberWithFloat:0.0f]];
    [animation setToValue:[NSNumber numberWithFloat:1.0f]];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    return animation;
}

-(CABasicAnimation *) buildOut{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    animation.duration = 2.0f;
    animation.beginTime = CACurrentMediaTime() + 5;
    [animation setFromValue:[NSNumber numberWithFloat:1.0f]];
    [animation setToValue:[NSNumber numberWithFloat:0.0f]];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    return animation;
}

- (IBAction)startAnimation:(id)sender {
    [self.textView.layer addAnimation:[self buildIn] forKey:@"transparent"];
    self.textView.layer.opacity = 1.0f;//I need to set the opacity to 1 after buildIn animation
    [self.textView.layer addAnimation:[self buildOut] forKey:@"transparent"];
    self.textView.layer.opacity = 0.0f;//ERROR: this is the final layer value, but I need to assign it after the build out animation, not here.
}

I think I can use a completion block to do it, but I can't find it.

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
remykits
  • 1,735
  • 4
  • 18
  • 20
  • maybe this answer will help you - http://stackoverflow.com/questions/11515647/objective-c-cabasicanimation-applying-changes-after-animation – Artem Shmatkov Mar 05 '13 at 08:57
  • Can't you just use `[UIView animateWithDuration...` instead? It seems you're just animating the opacity on `self.textView`... – jjv360 Mar 05 '13 at 09:18
  • @jjv360 well, this just a sample. I need to use Core Animation. – remykits Mar 05 '13 at 09:31

1 Answers1

0

If you set a delegate on your buildIn animation, then when it is finished the -animationDidStop:finished: message will be sent to the delegate. Then you can start your buildOut animation.

CAAnimation delegate reference

iain
  • 5,660
  • 1
  • 30
  • 51