2010-06-30 23 views
15

Estoy creando un menú y quiero que los botones "aparezcan", supongo, en la pantalla. Básicamente quiero comenzar en dimensiones de 0px y luego subir al tamaño completo de los botones. Puedo animar el alfa y la posición si quiero pero no puedo hacer las dimensiones y creo que es porque es una imagen en el botón.UIButton cambiar el tamaño con la animación

Si realizo un UIButtonTypeRoundRect, puede ver el botón animado detrás de la imagen pero la imagen es estática.

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button setBackgroundImage:[UIImage imageNamed:@"settings.png"] forState:UIControlStateNormal]; 
button.frame = CGRectMake(20, 20, 0, 0); 
button.alpha = 0; 
[self.view addSubview:button]; 
CGRect frame = button.frame; 
[UIView beginAnimations:@"button" context:nil]; 
[UIView setAnimationDuration:1]; 
button.alpha = 1; 
frame.size.width += 53; 
frame.size.height += 53; 
button.frame = frame; 
[UIView setAnimationDelegate:self]; 
[UIView commitAnimations]; 

Así que el Alpha funciona pero el cambio de tamaño no. También he jugado con stretchableImageWithLeftCapWidth para intentar darle contexto o algo así, pero fue en vano.

Saludos por su ayuda.

+0

¿Has intentado con el botón 'Scale To Fill'? – iwasrobbed

+0

sí, recuerda intentar eso en un momento. ¿Quién te robó? – Rudiger

Respuesta

32

¿Podría intentar usar el siguiente código?

button.transform = CGAffineTransformMakeScale(1.5,1.5); 
button.alpha = 0.0f; 

[UIView beginAnimations:@"button" context:nil]; 
[UIView setAnimationDuration:1]; 
    button.transform = CGAffineTransformMakeScale(1,1); 
    button.alpha = 1.0f; 
[UIView commitAnimations]; 

Su botón debería comenzar ligeramente más grande y luego volver a encoger. Si esto se escala correctamente, simplemente ajuste el factor de escala antes y después para adecuarlo.

+0

Gracias, le echaré un vistazo y le responderé. – Rudiger

+0

Eso no funcionó de inmediato. Botón = CGAffineTransformMakeScale (1.5,1.5); lanzó un error Pero básicamente puedes hacer el button.transform = CGAffineTransformMakeScale (1.5,1.5); y anima un aumento de escala como yo necesitaba. Gracias :) – Rudiger

+0

Cosas geniales: he modificado la muestra del código para cualquier otra persona que quiera usarla – davbryn

5

Actualizado para la Swift:

Puede insertar esto en lo IBAction que tienes atado a un botón para animar cuando es tocado.

// animate button 
    // play with the scale (1.25) to adjust to your liking 
    yourButton.transform = CGAffineTransformMakeScale(1.25, 1.25) 
    yourButton.alpha = 0.0 

    UIView.beginAnimations("yourButton", context: nil) 
    // play with the animationDuration to adjust to your liking 
    UIView.setAnimationDuration(0.25) 
    yourButton.transform = CGAffineTransformMakeScale(1.0, 1.0) 
    yourButton.alpha = 1.0 
Cuestiones relacionadas