Create a shake animation for a UIAlertView

孤街浪徒 提交于 2019-12-02 10:41:17

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!