0

我需要根据某些条件为不同的 CATextLayers 设置动画。这就是为什么我编写了一个接受图层并对其进行动画处理的方法。这里是:

- (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];
        }
4

1 回答 1

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"];       

}

但老实说,我不明白为什么那条线确实使它起作用。我想如果不重置前景色,动画应该会闪烁,前景色会恢复到初始值。实际上,尽管屏幕上绝对没有发生任何事情。添加后一切正常。

于 2013-12-12T07:34:16.343 回答