Reputation: 3580
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?
Upvotes: 3
Views: 2678
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
Reputation: 492
ShakingAlertView is a UIAlertView subclass that implements this functionality.
Disclaimer: I am the developer of ShakingAlertView
Upvotes: 1
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