2009-11-11 12 views
6

Estoy tratando de hacer vibrar/agitar un elemento de IU, es decir, un control, como si hubiera sido golpeado, y está resonando. Puedo usar Core Animation agitarlo vertical, horizontal o ambos, por un píxel:¿La mejor manera de hacer vibrar un elemento de UI?

CABasicAnimation* rotationXAnimation; 
rotationXAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"]; 
rotationXAnimation.fromValue = [NSNumber numberWithFloat: ((UIControl *) sender).center.y-1.0 ]; 
rotationXAnimation.toValue = [NSNumber numberWithFloat: ((UIControl *) sender).center.y+1.0 ]; 
rotationXAnimation.duration = 0.2; 
rotationXAnimation.cumulative = NO; 
rotationXAnimation.repeatCount = 10.0; 
rotationXAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 

[((UIControl *) sender).layer addAnimation:rotationXAnimation forKey:@"upDownAnimation"]; 

O puedo girar por un pequeño arco de ida y vuelta alrededor del eje z (-M_PI * 0,02 y M_PI * 0.02) :

CABasicAnimation* rotationAnimation; 
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
rotationAnimation.fromValue = [NSNumber numberWithFloat: -M_PI * 0.02 ]; 
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 0.02 ]; 
rotationAnimation.duration = 0.2; 
rotationAnimation.cumulative = NO; 
rotationAnimation.repeatCount = 10.0; 
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 

[((UIControl *) sender).layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 

Ninguno de ellos parece tan agradable. ¿Alguien tiene algunas buenas alternativas? Agradecería especialmente las geniales ideas de KeyPath para probar.

Gracias!

Actualización:

El siguiente, donde estoy Contratantes y ampliar el control, es mejor, pero todavía estoy en busca de otras ideas.

CABasicAnimation* scaleAnimation; 
scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 
scaleAnimation.fromValue = [NSNumber numberWithFloat: 0.95 ]; 
scaleAnimation.toValue = [NSNumber numberWithFloat: +1.05 ]; 
scaleAnimation.duration = 0.1; 
scaleAnimation.cumulative = NO; 
scaleAnimation.repeatCount = 10.0; 
scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 

[((UIControl *) sender).layer addAnimation:scaleAnimation forKey:@"scaleAnimation"]; 

Respuesta

6

estoy temblando uno de mis entrada-Views después de una entrada de usuario incorrecto usando:

- (void)earthquake:(UIView*)itemView 
{ 
    CGFloat t = 2.0; 
    CGAffineTransform leftQuake = CGAffineTransformTranslate(CGAffineTransformIdentity, t, -t); 
    CGAffineTransform rightQuake = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, t); 

    itemView.transform = leftQuake; // starting point 

    [UIView beginAnimations:@"earthquake" context:itemView]; 
    [UIView setAnimationRepeatAutoreverses:YES]; // important 
    [UIView setAnimationRepeatCount:4]; 
    [UIView setAnimationDuration:0.07]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(earthquakeEnded:finished:context:)]; 

    itemView.transform = rightQuake; // end here & auto-reverse 

    [UIView commitAnimations]; 
} 

- (void)earthquakeEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
{ 
    if ([finished boolValue]) 
    { 
     UIView* item = (UIView *)context; 
     item.transform = CGAffineTransformIdentity; 
    } 
} 

Esto se ve muy natural, me dieron el código de algún Internet-hilo que No puedo recordar

4

Puede usar una animación de fotograma clave en la posición de capa. Esto es subjetivo, por supuesto, porque no estoy seguro de lo que considera "agradable". La animación de sacudidas que recibe cuando ingresa su contraseña de OS X incorrectamente, la duplico usando Core Animation en este blog post on Cocoa Is My Girlfriend. No estoy seguro de si eso es lo que está buscando, pero esta es una alternativa.

Saludos,

+0

esto es sólo en una dirección - de lado a lado - ¿no? – mahboudz

+0

Sí. Pero puede crear una animación de fotograma clave que utiliza una ruta de la misma manera que anima el campo 'posición' que es un CGPoint. –

2

una actualización de Bersaelor's poco agradable respuesta sacudida aquí para utilizar UIView animaciones de bloques en su lugar, algunas personas no siempre saben que pueden utilizar auto-reverse-y repetir el recuento de éstos:

self.transform = CGAffineTransformMakeTranslation(2.0, -2.0); 
[UIView animateWithDuration:0.07 
         delay:0.0 
        options:UIViewAnimationOptionAutoreverse 
       animations:^{ 
        UIView.animationRepeatCount = 4; 
        self.transform = CGAffineTransformMakeTranslation(-2.0, 2.0); 
       } 
       completion:^(BOOL finished){ 
        self.transform = CGAffineTransformIdentity; 
       }]; 
Cuestiones relacionadas