我想构建一个图层并在5秒后构建它。但我不知道如何将最终值分配给图层。我的代码如下:
-(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.
}
我想我可以使用完成块来完成它,但我无法找到它。
答案 0 :(得分:0)
如果您在buildIn
动画上设置了委托,那么当它完成时,-animationDidStop:finished:
消息将被发送给委托。然后,您可以开始buildOut
动画。