我需要根据某些条件动画不同的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];
}
显示如何声明并初始化CATextLayers? – Unheilig
同时显示你在哪里以及如何调用此方法。 – Snowman
检查你的文本参数是否为零 –