为UIAlertView创建震动动画 [英] Create a shake animation for a UIAlertView

查看:65
本文介绍了为UIAlertView创建震动动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行 UIAlertView 摇动。我已经制作了 alertView 来保持按钮单击,但是摇动动画不起作用。这是我的东西:

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];
                                  }];
}

我尝试放置 NSLog -(无效)中摇晃... ,我得到了输出。因此,为什么 alertView 不会摇晃,我的代码肯定有问题。

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.

推荐答案

我创建了一种幻觉,使Alertview看起来像在摇晃。前提是您必须设置关于alertview(包括深色背景屏幕)应抖动的程度的参数。

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.

由于您无法使用UIAlertView的框架进行更改,因此请继续

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]; 

我建议改用自定义创建的UIAlertView,同时也很少有开源库可用。

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

这篇关于为UIAlertView创建震动动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆