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?
Asked
Active
Viewed 2,675 times
3

cdmh
- 3,294
- 2
- 26
- 41

junaidsidhu
- 3,539
- 1
- 27
- 49
3 Answers
2
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];
}];
}

Community
- 1
- 1

Paresh Navadiya
- 38,095
- 11
- 81
- 132
-
I like this approach, it's clean. Format a bit better/more consistently your code, though. +1. – Oct 31 '12 at 08:30
-
i try this but my Alert shake and then disappear. I dont want it to disappear – junaidsidhu Oct 31 '12 at 08:49
1
ShakingAlertView is a UIAlertView subclass that implements this functionality.
Disclaimer: I am the developer of ShakingAlertView

lukestringer90
- 492
- 7
- 16
0
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"];
}

ArtFeel
- 11,701
- 4
- 29
- 41

Kamar Shad
- 6,089
- 1
- 29
- 56