user3290180
user3290180

Reputation: 4410

How to do text field shake effect?

I'm trying to imitate the effect at login in Mac OSX: when you put a wrong password the textfield begin a nice shake effect. I tried this but I'm not satisfied.

-(void)shakeText:(UITextField*)textField
{
     [UIView animateWithDuration:0.25
                      delay:0.0
                    options:UIViewAnimationOptionAutoreverse
                 animations:^{textField.transform =CGAffineTransformMakeTranslation(5, 0);}
                 completion:^(BOOL finished) {
                     [UIView animateWithDuration:0.5
                                           delay:0.0
                                         options:UIViewAnimationOptionAutoreverse
                                      animations:^{textField.transform = CGAffineTransformMakeTranslation(5, 0);}
                                      completion:nil];
                 }];
}

Is there a function in iOS to do it properly?

Upvotes: 0

Views: 885

Answers (1)

Asaf
Asaf

Reputation: 2168

Please try the following code:

// Change the 10 & -10 values as you wish
CGAffineTransform leftWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-10.0));
CGAffineTransform rightWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(10.0));

textField.transform = leftWobble;

[UIView animateWithDuration:0.25
        delay:0
        options:(UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse) animations:^{

        [UIView setAnimationRepeatCount:6]; // Change this value as you want

        textField.transform = rightWobble;

} completion:^(BOOL finished){

        textField.transform = CGAffineTransformIdentity;

}];

Upvotes: 2

Related Questions