条件によっては、さまざまな 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 です。問題は、このコードが「テキスト」レイヤーをアニメーション化しないことです。
(メソッドが色を受け取ることに注意しないでください。テストのために赤と黒を一時的に入れています。)
テストを開始しました。このメソッド内にレイヤーを作成してから、この新しく作成したメソッドをアニメーション化しようとすると、問題なく動作します。すべてのレイヤーが作成されるメソッドにコードをコピーして貼り付け、そのうちの 1 つをアニメーション化しようとすると、問題なく動作します。
しかし、さまざまな瞬間にさまざまなレイヤーをアニメーション化する必要があるため、この機能は不可欠だと考えています。
私が間違っていることを説明してください。前もって感謝します。
更新: レイヤーを作成して 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];
}