Create a shake animation for a UIAlertView

942 views Asked by At

I'm trying to make a UIAlertView shake. I already made the alertView to stay on button click, but the shake animation is not working. Here is what I have:

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        self.direction = 1;
        self.shakes = 0;
        [self shake:aUIView];
    }
}

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

I tried putting an NSLog in - (void)shake... and I got an output. So there must be something wrong with my code as to why the alertView isn't shaking.

2

There are 2 answers

1
AudioBubble On BEST ANSWER

I have created a illusion that makes alertview appear like it is shaking. Provided you must set the parameters about upto how much extent alertview(including that dark background screen) should shake.

Since you can't alter with the frames of UIAlertView so go down on CGTranformations.

UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:@"Title"
                                                 message:@"Message:"
                                                delegate:self cancelButtonTitle:@"Cancel"
                                       otherButtonTitles:@"Done", nil];

[dialog setAlertViewStyle:UIAlertViewStylePlainTextInput];
[dialog show];


UIView *dillusionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[dillusionView setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:dillusionView];


[UIView animateKeyframesWithDuration:1.0 delay:0.0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{
    [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
        dialog.window.transform = CGAffineTransformTranslate(dialog.transform, dialog.frame.origin.x - 10, dialog.frame.origin.y);
    }];
    [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
        dialog.window.transform = CGAffineTransformTranslate(dialog.transform, dialog.frame.origin.x + 10, dialog.frame.origin.y);
    }];
} completion:nil]; 

I would recommend using Custom created UIAlertView instead, also there are few opensource libraries available.

0
Vineeth Joseph On

You can use UIKit Dynamics for animated effects in iOS. Here is a tutorial which using UIKit Dynamics for UIAlertView shake animation. For more you can refer this tutorial also which is in swift.