2013-12-11 3 views
0

일부 조건에 따라 다른 CATextLayer를 애니메이션화해야합니다. 그래서 레이어를 허용하고 애니메이션을 적용하는 메서드를 작성했습니다. 여기에 있습니다 :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"]; 

"text"는 다른 방법으로 하위 레이어에 생성, 초기화 및 추가 된 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; 
    } 

에 배치하는 방법, 그것을 작동합니다. 그리고이 같은 방법으로 레이어를 통과 할 때 작동이 중지 :

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

} 

그러나에 :

여기에 올바른 방법 : 내가 그것을 작동하도록 관리하는 것

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]; 
     } 
+0

CATextLayers를 선언하고 초기화하는 방법을 보여주십시오. – Unheilig

+0

이 메소드를 호출하는 위치와 방법도 표시하십시오. – Snowman

+0

텍스트 매개 변수가 nil이 아닌지 확인하십시오. –

답변

0

솔직히 말하면, 왜 그 라인이 효과가 있는지 이해할 수 없습니다. 전경색을 재설정하지 않으면 애니메이션이 깜박이고 전경색이 초기 값으로 돌아갑니다. 현실에서는 아무 것도 화면에 아무 것도 발생하지 않았습니다. 이 모든 것을 추가하면 정상적으로 작동합니다.