2010-09-08 12 views

Respuesta

19

llamada

button.transform = CGAffineTransformMakeScale(1.1,1.1); 

En botón pulsado manejador.

O si lo desea escalar con la animación:

[UIView beginAnimations:@"ScaleButton" context:NULL]; 
[UIView setAnimationDuration: 0.5f]; 
button.transform = CGAffineTransformMakeScale(1.1,1.1); 
[UIView commitAnimations]; 
+1

bien fresco y cómo puedo combinar esto con un evento de contacto? – Tronic

+0

lo siento, no entiendo lo que quiere decir exactamente ... – Vladimir

+0

quiero decir, cómo puedo combinar este código con el evento, al presionar un botón. – Tronic

15

Para completar la respuesta, el escalado botón (y reiniciar) se pueden colocar en los métodos de esta manera:

// Scale up on button press 
- (void) buttonPress:(UIButton*)button { 
    button.transform = CGAffineTransformMakeScale(1.1, 1.1); 
    // Do something else 
} 

// Scale down on button release 
- (void) buttonRelease:(UIButton*)button { 
    button.transform = CGAffineTransformMakeScale(1.0, 1.0); 
    // Do something else 
} 

y conectados con eventos del botón de esta manera:

[btn addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchDown]; 
[btn addTarget:self action:@selector(buttonRelease:) forControlEvents:UIControlEventTouchUpInside]; 
[btn addTarget:self action:@selector(buttonRelease:) forControlEvents:UIControlEventTouchUpOutside]; 

NOTA 1: Establecer los valores de CGAffineTransformMakeScale en 1.0 no los mantiene en sus valores alterados (es decir, no se multiplica 1.1 por 1.0), sino que los establece de nuevo en la escala original del objeto.

NOTA2: No olvide los dos puntos : en el selector, ya que permite el paso del emisor como un parámetro para el método de recepción. En este caso, nuestros métodos reciben un UIButton y se declararían como tales en la interfaz (archivo .h).

+4

No se olvide de 'UIControlEventTouchCancel' y' UIControlEventTouchDragExit' – Tim

+0

super.Gracias tanto ... Perfecto –

2

Esto es lo que yo uso

-(IBAction)heartButtonTapped:(UIButton*)sender { 
    [sender setSelected:!sender.isSelected]; 


    [UIView animateWithDuration:0.6 delay:0.0 options:UIViewAnimationOptionAutoreverse animations:^{ 
     sender.transform = CGAffineTransformMakeScale(1.5,1.5); 
    } completion:^(BOOL finished) { 
     sender.transform = CGAffineTransformMakeScale(1,1); 
    }]; 
} 
+0

Solución simple, gracias, encontró un pequeño problema: cuando la animación de autoreverse está completa, el botón cambia de tamaño de 1.5x a 1x. Puede evitarse con una ligera modificación, se publica el código completo por separado. – Thiru

2

código ligeramente modificado respecto a las @ ibm123 que evita problema repentino cambio de tamaño.

- (IBAction) buttonTapAction:(UIButton *) sender { 
[self animatePressedDown:sender duration:0.6 zoom:1.5]; 

}

- (void)animatePressedDown:(UIButton *) sender duration:(double) t zoom:(double) zoomX { 
[UIView animateWithDuration:t delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ 
    sender.transform = CGAffineTransformMakeScale(zoomX,zoomX); 
} completion:^(BOOL finished) { 
    [UIView animateWithDuration:t delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ 
     sender.transform = CGAffineTransformMakeScale(1,1); 
    } completion:nil]; 
}]; 

}

0

Swift:

button.transform = CGAffineTransform.init(scaleX: 1.0, y: 1.0)