0

我想构建一个层并在 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.
}

我想我可以使用完成块来做到这一点,但我找不到它。

4

1 回答 1

0

如果您在buildIn动画上设置了代理,那么当动画完成时,-animationDidStop:finished:消息将发送给代理。然后你就可以开始你的buildOut动画了。

CAAnimation 委托参考

于 2013-03-06T14:30:37.150 回答