2012-01-23 18 views
8

¿Hay alguna manera de duplicar la animación del botón sir glow? Se ve absolutamente precioso, pero no tengo ni idea en este momento de cómo empezar ... ¿hay png preformateados en línea que se roten? o se hace con CoreAnimation?Recreate Siri Button Glow Animation

Respuesta

9

Creo que la animación de Siri se hace con CAEmitterLayer y CAEmitterCell y luego se anima con animación central, pero podría estar totalmente equivocado. Aquí hay un código que imita el efecto:

// create emitter layer 
self.emitterLayer = [CAEmitterLayer layer]; 
self.emitterLayer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height); 

self.emitterLayer.emitterMode = kCAEmitterLayerOutline; 
self.emitterLayer.emitterShape = kCAEmitterLayerLine; 
self.emitterLayer.renderMode = kCAEmitterLayerAdditive; 
[self.emitterLayer setEmitterSize:CGSizeMake(4, 4)]; 

// create the ball emitter cell 
CAEmitterCell *ball = [CAEmitterCell emitterCell]; 
ball.color = [[UIColor colorWithRed:111.0/255.0 green:80.0/255.0 blue:241.0/255.0 alpha:0.10] CGColor]; 
ball.contents = (id)[[UIImage imageNamed:@"ball.png"] CGImage]; // ball.png is simply an image of white circle 
[ball setName:@"ball"]; 

self.emitterLayer.emitterCells = [NSArray arrayWithObject:ball]; 
[self.view.layer addSublayer:self.emitterLayer]; 

float factor = 1.5; // you should play around with this value 
[self.emitterLayer setValue:[NSNumber numberWithInt:(factor * 500)] forKeyPath:@"emitterCells.ball.birthRate"]; 
[self.emitterLayer setValue:[NSNumber numberWithFloat:factor * 0.25] forKeyPath:@"emitterCells.ball.lifetime"]; 
[self.emitterLayer setValue:[NSNumber numberWithFloat:(factor * 0.15)] forKeyPath:@"emitterCells.ball.lifetimeRange"]; 


// animation code 
CAKeyframeAnimation* circularAnimation = [CAKeyframeAnimation animationWithKeyPath:@"emitterPosition"]; 
CGMutablePathRef path = CGPathCreateMutable(); 
CGRect pathRect = CGRectMake(0, 0, 200, 200); // define circle bounds with rectangle 
CGPathAddEllipseInRect(path, NULL, pathRect); 
circularAnimation.path = path; 
CGPathRelease(path); 
circularAnimation.duration = 2; 
circularAnimation.repeatDuration = 0; 
circularAnimation.repeatCount = 3; 
circularAnimation.calculationMode = kCAAnimationPaced; 
[self.emitterLayer addAnimation:circularAnimation forKey:@"circularAnimation"]; 

clases CAEmitterCell y CAEmitterLayer tienen muchas propiedades a fin de comprobar la documentación para más.

+0

self.fireEmitter falta el contexto aquí – Pascalius

+0

He olvidado editar esas líneas al copiar el código. Debería haber sido self.emitterLayer. Lo he arreglado – jlajlar

+1

Olvidó mencionar que "ball.png" debería tener un tamaño de 5x5 píxeles ... – Idan

6

Le sugiero que lo haga con PNG que se muestran uno por uno, de modo que se obtiene una animación suave. Es mucho más fácil que programar una animación codificada. El botón ya ha sido recreado por Arron Hunt: Siri Button Photoshop File

Btw. Una animación de sprites es muy fácil de implementar:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSArray * imageArray = [[NSArray alloc] initWithObjects: 
          [UIImage imageNamed:@"1.png"], 
          [UIImage imageNamed:@"2.png"], 
          [UIImage imageNamed:@"3.png"], 
          [UIImage imageNamed:@"4.png"], 
          [UIImage imageNamed:@"5.png"], 
          [UIImage imageNamed:@"6.png"], 
          [UIImage imageNamed:@"7.png"], 
          [UIImage imageNamed:@"8.png"], 
          [UIImage imageNamed:@"9.png"], 
          [UIImage imageNamed:@"10.png"], 
          [UIImage imageNamed:@"11.png"], 
          [UIImage imageNamed:@"12.png"], 
          nil]; 
    UIImageView * ryuJump = [[UIImageView alloc] initWithFrame: 
     CGRectMake(100, 125, 150, 130)]; 
    ryuJump.animationImages = imageArray; 
    ryuJump.animationDuration = 1.1; 
    ryuJump.contentMode = UIViewContentModeBottomLeft; 
    [self.view addSubview:ryuJump]; 
    [ryuJump startAnimating]; 
} 

Fuente: http://www.icodeblog.com/2009/07/24/iphone-programming-tutorial-animating-a-game-sprite/

+0

Tienes un punto ... Así que implementar suficientes PNG para una animación de rotación sería una manera. Siempre pensé que tales cosas deberían hacerse con CoreAnimation porque es más fácil de modificar en ese momento. ¡El enlace es genial! Voy a echar un vistazo a eso! – konturgestaltung

+0

Eche un vistazo a mi respuesta editada para un código de muestra que hace una animación de sprite. Buena suerte – Sbhklr

+0

@Lightforce, ¿Alguien puede tener un conjunto de imágenes PNG para este archivo PSD? No tengo ningún conocimiento de Photoshop para extraer las imágenes. ¿Alguien puede subirlo a alguna parte para que sea fácil de usar por todos? . – user1227928

1

UIImageView tiene una propiedad llamada animationImages que se pueden utilizar para especificar una lista de imágenes que se reproducirá en secuencia, como un GIF animado . Esa es probablemente la forma más fácil de hacerlo si desea controlar con precisión el efecto.

Algo similar también podría hacerse con CoreAnimation utilizando una sola imagen y animando su propiedad view.transform utilizando CGAffineTransformMakeRotation (ángulo).

+0

Esa fue mi primera idea también ... hacer una imagen brillante con un degradado y alfa de 0 a la vez y en forma de círculo y luego rotar esa cosa ... pero no estoy seguro de si eso produciría ese efecto brillante. .. porque todavía se vería un poco estático? – konturgestaltung

+0

También es posible que desee variar el alfa a lo largo del tiempo, por lo tanto, disminuya la entrada y la salida del alfa a medida que gira para que parezca que pulsa un poco. –

+0

nice ..... that should do it ..... ¡Pruébalo! – konturgestaltung