2013-12-11 40 views
0

我需要根据某些条件动画不同的CATextLayers。这就是为什么我写了一个方法来接受图层并对其进行动画处理。这里是:CABasicAnimation仅适用于使用相同方法创建的图层

- (void) animateText:(CATextLayer*) text withColor: (CGColorRef) color andDuration: (NSTimeInterval) duration 
{ 
    CABasicAnimation *colorAnimation = [CABasicAnimation 
             animationWithKeyPath:@"foregroundColor"]; 
    colorAnimation.duration = duration; 
    colorAnimation.fillMode = kCAFillModeForwards; 
    colorAnimation.removedOnCompletion = NO; 
    colorAnimation.fromValue = (id)[UIColor redColor].CGColor; 
    colorAnimation.toValue = (id)[UIColor blackColor].CGColor; 
    colorAnimation.timingFunction = [CAMediaTimingFunction 
            functionWithName:kCAMediaTimingFunctionLinear]; 
    [text addAnimation:colorAnimation 
        forKey:@"colorAnimation"]; 

“文本”是一个CATextLayer创建,初始化并添加到其他方法的子层。 问题是此代码不会使“文本”图层生成动画。

(请,不注重该方法接收的颜色。我暂时把红色和黑色进行测试。)

我开始测试。如果我在这个方法中创建一个图层,然后尝试为这个新创建的方法创建动画,那么它就可以正常工作。 如果我将代码复制粘贴到创建所有图层的方法中并尝试对其中的一个进行动画处理 - 它也可以正常工作。

但是我需要在不同的时刻动画不同的图层,所以我觉得这个功能是必不可少的。

请解释我做错了什么。 预先感谢您。

更新: 这里是如何的层被创建并放置到UIView的

//creating 110 text layers 

    CGFloat leftPoint = 0; 
    CGFloat topPoint = 0; 

    for (int i=0; i<11; ++i) 
    { 
     for (int j=0; j<10; ++j) 
     { 
      CATextLayer *label = [[CATextLayer alloc] init]; 
      [label setString:[_labels objectAtIndex:j+(i*10)]]; // _labels contains letters to display 
      [label setFrame:CGRectMake(leftPoint, topPoint, _labelWidth, _labelHeight)]; // _labelWidth and _labelHeight are valid numbers 
      [label setAlignmentMode:kCAAlignmentCenter]; 
      [label setForegroundColor:_textColor]; // _textColor is a CGColorRef type 
      [label setFont:(__bridge CFTypeRef)(_font.fontName)]; // font is UIFont type 
      [label setFontSize:[_font pointSize]]; 

      [_textViews addObject:label]; // saving to internal array variable 
      [[self layer] addSublayer:label]; 

      leftPoint += _labelWidth; 
     } 
     leftPoint = 0; 
     topPoint += _labelHeight; 
    } 

如果我把这样的代码在我的动画同样的方法,它的工作原理。 它停止工作,当我在这样的方法传递层:

for (int i = 0; i<labelsToHighlight.count; ++i) // labelsToHighlight is an array containing indexes of the CATextLayers which I need to animate 
     { 
      NSUInteger index = [[labelsToHighlight objectAtIndex:i] intValue]; 
      [self animateText:[_textViews objectAtIndex:index] withColor: _highlightedTextColor andDuration:_redrawAnimationDuration]; 
     } 
+0

显示如何声明并初始化CATextLayers? – Unheilig

+0

同时显示你在哪里以及如何调用此方法。 – Snowman

+0

检查你的文本参数是否为零 –

回答

0

看来我设法使其工作:

下面是正确的方法:

- (void) animateText:(CATextLayer*) text fromColor: (CGColorRef) fromColor toColor: (CGColorRef) toColor andDuration: (NSTimeInterval) duration 
{ 
    CABasicAnimation *colorAnimation = [CABasicAnimation 
             animationWithKeyPath:@"foregroundColor"]; 
    colorAnimation.duration = duration; 
    colorAnimation.fillMode = kCAFillModeForwards; 
    colorAnimation.removedOnCompletion = NO; 
    colorAnimation.fromValue = (__bridge id)fromColor; 
    colorAnimation.toValue = (__bridge id) toColor; 
    colorAnimation.timingFunction = [CAMediaTimingFunction 
            functionWithName:kCAMediaTimingFunctionLinear]; 


    [text setForegroundColor:toColor]; // This is what I have actually added 
    [text addAnimation:colorAnimation forKey:@"colorAnimation"];  

} 

但要完全诚实,我不明白为什么这条线确实有效。 我想,如果不重置前景色,动画应该眨眼睛,前景色将返回到其初始值。事实上,尽管屏幕上没有任何事情发生。 添加后,一切正常。

相关问题