views:

249

answers:

2

I'm trying to vibrate/shake a UI element, namely a control, as if it were struck, and is resonating. I can use Core Animation to shake it vertically, horizontally or both, by one pixel:

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

Or I can rotate by a small arc back and forth around the z axis (-M_PI * 0.02 and 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"];

None of these look that pleasing. Does anyone have some good alternatives? I would especially appreciate cool keyPaths ideas to try.

Thanks!

Update:

The following, where I am contracting and expanding the control, is better, but I'm still looking for other 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"];
+3  A: 

You can use a keyframe animation on the layer position. This is subjective of course because I'm not sure what you consider "pleasing". The shake animation that you get when you enter your OS X password incorrectly I duplicated using Core Animation in this blog post on Cocoa Is My Girlfriend. Not sure if that is what you're going for, but this is one alternative.

Best regards,

Matt Long
This is only in one direction - side-to-side - right?
mahboudz
Yes. But you can create a keyframe animation that uses a path in the same way animating the 'position' field which is a CGPoint.
Matt Long
+1  A: 

I'm shaking one of my Input-Views after a wrong user input using:

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

This looks quite natural, I got this code from some internet-thread that I can't remember.

Bersaelor