2014-07-12 3 views
0

레이어가 있고 drawInContext:drawInRect:이라는 속성이있는 문자열을 그립니다. 다음은 레이어를 초기화하는 방법입니다.NSMutableAttributedString 애니메이션 애니메이션 - 문자열이 사라짐

+ (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; 
} 

문자열의 색상을 애니메이트하려면 3 가지 다이내믹 특성이 있습니다.

@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; 

/* ... */ 

전경 색상을 설정합니다.

/* 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을 추가하면 심볼이 사라집니다.

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 속성을 어떻게 애니메이트합니까?

답변

0

이것은 애니메이션이 시작되면 호출되기 때문에 - (id)initWithLayer:(id)layer 메서드를 구현하는 간단한 질문이었습니다.

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

이제 특성이 이어지고 예상대로 결과가 나옵니다.