2011-09-21 5 views
7

실제로 iOS 응용 프로그램에서 UILabel에 애니메이션을 적용하는 데 문제가 있습니다. 2 일 동안 웹에서 코드 스 니펫을 검색 한 후에도 결과가 없습니다.iOS 응용 프로그램에서 CoreAnimation/QuartzCore로 UILabel에 애니메이션 적용

내가 발견 한 모든 샘플은 UIImage를 애니메이트하는 방법에 관한 것이 었습니다. UIView에 하위보기로 추가했습니다. UILabel에 애니메이션을 적용하는 것에 대한 좋은 예가 있습니까? 나는 다음과 같이 알파 속성을 설정하여 깜빡이는 애니메이션을위한 좋은 해결책을 발견 :

내 기능 :

- (void)blinkAnimation:(NSString *)animationID finished:(BOOL)finished target:(UIView *)target 
{ 
    NSString *selectedSpeed = [[NSUserDefaults standardUserDefaults] stringForKey:@"EffectSpeed"]; 
    float speedFloat = (1.00 - [selectedSpeed floatValue]); 

    [UIView beginAnimations:animationID context:target]; 
    [UIView setAnimationDuration:speedFloat]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(blinkAnimation:finished:target:)]; 

    if([target alpha] == 1.0f) 
     [target setAlpha:0.0f]; 
    else 
     [target setAlpha:1.0f]; 
    [UIView commitAnimations]; 
} 

가 UILabel의 내 함수를 호출 :

[self blinkAnimation:@"blinkAnimation" finished:YES target:labelView]; 

그러나 방법을 펄스 또는 스케일 애니메이션?

답변

13

불행히도 글꼴 크기는 NSView의 애니메이션 속성이 아닙니다. UILabel의 크기를 조절하려면 CAKeyframeAnimation을 사용하여 고급 Core Animation 기술을 사용해야합니다 : 코드에서

  1. 가져 오기 프로젝트에 QuartzCore.framework 및 #import <QuartzCore/QuartzCore.h>을.
  2. 키 프레임을 추가 할 수있는 새로운 CAKeyframeAnimation 개체를 만듭니다.
  3. 크기 조정 작업을 정의하는 CATransform3D 값을 만듭니다. 3D 부품으로 혼동하지 마세요.이 객체를 사용하여 레이어에 변형을 변환합니다.
  4. setValues 메서드를 사용하여 CAKeyframeAnimation 개체에 애니메이션을 추가하여 애니메이션에서 키 프레임 중 하나를 변환합니다.

    // Create the keyframe animation object 
    CAKeyframeAnimation *scaleAnimation = 
        [CAKeyframeAnimation animationWithKeyPath:@"transform"]; 
    
    // Set the animation's delegate to self so that we can add callbacks if we want 
    scaleAnimation.delegate = self; 
    
    // Create the transform; we'll scale x and y by 1.5, leaving z alone 
    // since this is a 2D animation. 
    CATransform3D transform = CATransform3DMakeScale(1.5, 1.5, 1); // Scale in x and y 
    
    // Add the keyframes. Note we have to start and end with CATransformIdentity, 
    // so that the label starts from and returns to its non-transformed state. 
    [scaleAnimation setValues:[NSArray arrayWithObjects: 
            [NSValue valueWithCATransform3D:CATransform3DIdentity], 
            [NSValue valueWithCATransform3D:transform], 
            [NSValue valueWithCATransform3D:CATransform3DIdentity], 
            nil]]; 
    
    // set the duration of the animation 
    [scaleAnimation setDuration: .5]; 
    
    // animate your label layer = rock and roll! 
    [[self.label layer] addAnimation:scaleAnimation forKey:@"scaleText"]; 
    
    :
  5. 마지막으로 그 setDuration 방법 호출하여 애니메이션의 지속 기간을 설정, [[yourLabelObject layer] addAnimation:yourCAKeyframeAnimationObject forKey:@"anyArbitraryString"]

마지막 코드는 다음과 같이 보일 수있는 사용하여 레이블의 레이어에 애니메이션을 추가

반복되는 "펄스"애니메이션을 연습 문제로 남겨 두겠습니다. 힌트 : animationDidStop 메서드가 필요합니다!

다른 참고 사항 - CALayer 애니메이션 가능 속성 ("transform"이 하나임)의 전체 목록은 here입니다. 행복한 트위닝!

+0

답장을 보내 주셔서 감사합니다. 타이머로 CABasicAnimation을 사용하여 이미 문제를 해결했습니다. 당신의 코드도 잘 동작 할 것이므로 받아 들일 것입니다. ;) – DevZarak

관련 문제