junaidsidhu
junaidsidhu

Reputation: 3580

Shaking the UIAlertView if user press Submit button when the textFields are empty

I want to shake the UIAlertView if a user presses the submit button without entering any data in the textFields. Is it possible in iOS?

enter image description here

Upvotes: 3

Views: 2678

Answers (3)

Mohammad Kamar Shad
Mohammad Kamar Shad

Reputation: 6067

Here the Code Call This Methods As validation Gets Failed

- (void)animateView 
{
  CAKeyframeAnimation *animation;
        animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
        animation.duration = 2.0;
        animation.cumulative = NO;
        animation.repeatCount = MAXFLOAT;
        animation.values = [NSArray arrayWithObjects:
                            [NSNumber numberWithFloat: 0.0], 
                            [NSNumber numberWithFloat: DEGREES_TO_RADIANS(-4.0)], 
                            [NSNumber numberWithFloat: 0.0],
                            [NSNumber numberWithFloat: DEGREES_TO_RADIANS(4.0)],
                            [NSNumber numberWithFloat: 0.0], nil];
        animation.fillMode = kCAFillModeBoth;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        animation.removedOnCompletion = NO;
        [[alertView layer] addAnimation:animation forKey:@"effect"];
}

Call this Animation whenever you want to stop animation

- (void)stopAnimatiomn
{
    [[alertView layer] removeAnimationForKey:@"effect"];
}

Upvotes: 0

lukestringer90
lukestringer90

Reputation: 492

ShakingAlertView is a UIAlertView subclass that implements this functionality.

Disclaimer: I am the developer of ShakingAlertView

Upvotes: 1

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

Firstly add inside header file add

int direction;
int shakes;

For preventing UIAlertView from dismissing. refer keep-uialertview-displayed link. also refer prevent-alertview-dismissal link.

Use UIAlertView Delegate:

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  if (buttonIndex == 0)
    //do here something
  else if (buttonIndex == 1){
    if(txtField.text.length == 0 || txtField1.text.length == 0) //check your two textflied has value
    {
      direction = 1;
      shakes = 0;
    }
  }
}

Add this method:

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

Refer more here about animation

Upvotes: 2

Related Questions