动画NSMutableAttributedString颜色-字符串消失 [英] Animating NSMutableAttributedString color - the string disappears
问题描述
我有一个图层,并在其 drawInContext:
中用 drawInRect:
绘制了一个属性字符串。
I have a layer and in its drawInContext:
I draw an attributed string with drawInRect:
. Here's how I initialise the layer.
+ (id)layer
{
Character *layer = [[self alloc] init];
if (layer) {
NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"Fabada" size:72]};
layer.symbol = [[NSMutableAttributedString alloc] initWithString:@"X" attributes:attributes];
}
return layer;
}
我想为字符串的颜色设置动画,所以我具有三个动态属性。 / p>
I want to animate the string's color, so I have three dynamics properties.
@interface Character : CALayer
@property (nonatomic, strong) NSMutableAttributedString *symbol;
@property (nonatomic) int red;
@property (nonatomic) int green;
@property (nonatomic) int blue;
@end
@implementation Character
@dynamic red;
@dynamic green;
@dynamic blue;
/* ... */
在draw方法中,I
In the draw method, I set the foreground color.
/* Set symbol color */
NSRange range = {0, [self.symbol length]};
UIColor *foreground = [UIColor colorWithRed:self.red / 255.0
green:self.green / 255.0
blue:self.blue / 255.0
alpha:1];
[self.symbol addAttribute:NSForegroundColorAttributeName value:foreground range:range];
CGRect symbolRect; /* The frame */
[self.symbol drawInRect:symbolRect];
该字符会根据需要显示在屏幕上。但是,一旦我在图层上添加了 CABasicAnimation
,该符号就会消失。
The character appears on screen as desired. But once I add a CABasicAnimation
to the layer, the symbol just disappears.
CABasicAnimation *animation = [CABasicAnimation animation];
animation.duration = 10.0;
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
animation.delegate = self;
animation.fromValue = [NSNumber numberWithInt:0];
animation.toValue = [NSNumber numberWithInt:255];
[self.character addAnimation:animation forKey:@"blue"];
此症状表示什么?如何为 NSAttributedString
属性设置动画?
What does this symptom imply? How do I animate NSAttributedString
attributes?
推荐答案
这是一个简单的问题-(id)initWithLayer:(id)layer
方法的实现,因为一旦动画开始就调用它。
This was a simple question of implementing the - (id)initWithLayer:(id)layer
method, because it gets called once the animation begins.
- (id)initWithLayer:(id)layer
{
Character *temp = (Character *)layer;
self = [super initWithLayer:layer];
if (self) {
self.symbol = temp.symbol;
self.red = temp.red;
self.green = temp.green;
self.blue = temp.blue;
}
return self;
}
现在这些属性结转,结果如预期。
Now the properties carry over and the results are as expected.
这篇关于动画NSMutableAttributedString颜色-字符串消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!