无法控制UITableViewCell子图层的动画计时

时间:2014-01-25 17:51:43

标签: ios iphone uitableview core-animation camediatiming

CAMediaTiming协议定义属性timeOffset,它应该设置动画或动画层的附加时间偏移。通过设置layer.speed = 0,可以通过将layer.timeOffset设置为给定值来手动控制动画计时。

我设法在常规视图中执行此操作,然而当我尝试这样做时(设置图层的时间偏移量)当图层是UITableViewCell图层的后代时它没有任何效果。

这是一个快速摘录,以便您可以看到我想要实现的目标和不起作用的内容。

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(0, 0, 80, 80);
    layer.backgroundColor = [UIColor redColor].CGColor;
    layer.opacity = .1f;
    [cell.contentView.layer addSublayer:layer];

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    animation.toValue = (id) [NSNumber numberWithFloat:1.f];
    animation.duration = 1.f;
    [layer addAnimation:animation forKey:nil];
    layer.timeOffset = 0.7f;
    layer.speed = 0.f;

}

1 个答案:

答案 0 :(得分:0)

您必须先允许动画启动,然后才能控制其timeOffset。使用延迟非常小的块:

    CABasicAnimation* ba = [CABasicAnimation animationWithKeyPath:@"opacity"];
ba.removedOnCompletion = NO;
ba.duration = kAnimationDuration;
ba.autoreverses = YES;
ba.repeatCount = HUGE_VALF;
ba.fromValue = [NSNumber numberWithDouble:kInitialAlpha];
ba.toValue = [NSNumber numberWithDouble:kFinalAlpha];
[self.layer addAnimation:ba forKey:@"opacity"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    double offset = self.alphaOffset * ( kAnimationDuration / kNumAlphaOffsets );
    self.layer.timeOffset = offset;
});