JAHelia JAHelia - 4 years ago 100
iOS Question

animating UITextField to indicate a wrong password

how can I add an animation to a

to indicate wrong password exactly like the one in facebook app (at login screen) or the Mac OS X login box ?

thank you in advance.

Answer Source

Something like that

-(void)shake:(UIView *)theOneYouWannaShake
  [UIView animateWithDuration:0.03 animations:^
                                    theOneYouWannaShake.transform = CGAffineTransformMakeTranslation(5*direction, 0);
                                  completion:^(BOOL finished) 
                                    if(shakes >= 10)
                                      theOneYouWannaShake.transform = CGAffineTransformIdentity;
                                    direction = direction * -1;
                                    [self shake:theOneYouWannaShake];

So you need three more things: An int direction which is set to 1 before the shake is called an int shakes, which is set to 0 before the shake is called and a constant MAX_SHAKES which is as large as you like. Hope that helps.


call it like this:

  direction = 1;
  shakes = 0;
  [self shake:aUIView];

inside header file add

int direction;
int shakes;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download