1

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;

}
4

1 回答 1

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;
});
于 2015-03-02T02:18:29.710 回答