2014-01-25 19 views
1

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

,我设法做一个常规视图,但是当我尝试这样做(规定时间内层的偏移量)当层的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; 

} 
+0

它只是timeOffset不起作用,或者您能告诉我们更多关于正在发生的事情吗? –

+0

是的 - 在'CAMediaTiming'协议中定义的所有属性都可以工作,但'timeOffset' – wczekalski

回答

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; 
});