我想构建一个层并在 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.
}
我想我可以使用完成块来做到这一点,但我找不到它。