2011-09-21 8 views
7

De hecho, me he atascado en un problema al animar un UILabel en mi aplicación iOS. Después de 2 días de buscar fragmentos de código en la web, todavía no hay resultados.Animación de UILabel con CoreAnimation/QuartzCore en la aplicación iOS

Cada muestra que encontré fue sobre cómo animar UIImage, agregándolo como una subvista a UIView por capa. ¿Hay algún buen ejemplo sobre animar a UILabel? he encontrado una buena solución para una animación intermitente mediante el establecimiento de la propiedad alpha, así:

Mi función:

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

llamar a mi función en el UILabel:

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

Pero ¿qué hay un pulso, o una animación de escala?

Respuesta

13

Desafortunadamente, el tamaño de fuente no es una propiedad animable de NSView. Con el fin de escalar una UILabel, tendrá que utilizar técnicas más avanzadas Core Animation, utilizando CAKeyframeAnimation:

  1. Importe el QuartzCore.framework en su proyecto, y #import <QuartzCore/QuartzCore.h> en el código.
  2. Crea un nuevo objeto CAKeyframeAnimation al que puedes agregar tus fotogramas clave.
  3. Crea un valor CATransform3D que define la operación de escala (no te confundas con la parte 3D; utilizas este objeto para hacer las transformaciones en una capa).
  4. Haga la transformación uno de los fotogramas clave de la animación agregándolo al objeto CAKeyframeAnimation utilizando su método setValues.
  5. establecer una duración para la animación llamando a su método setDuration
  6. Por último, añadir la animación a la capa de la etiqueta utilizando [[yourLabelObject layer] addAnimation:yourCAKeyframeAnimationObject forKey:@"anyArbitraryString"]

El código final podría ser algo como esto:

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

Dejaré la animación repetitiva de "pulso" como un ejercicio para ti: insinuación, implica el método animationDidStop.

Otra nota: la lista completa de propiedades animables de CALayer (de las cuales "transforma" es una) se puede encontrar en here. ¡Interpolación feliz!

+0

Muchas gracias por su respuesta detallada. Ya resolví el problema, usando CABasicAnimation con un temporizador. Estoy seguro de que tu código también funcionará bien, así que obtienes la aceptación. ;) – DevZarak

Cuestiones relacionadas