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.