为 UIAlertView 创建抖动动画



我正在尝试UIAlertView摇晃。我已经alertView保持按钮单击,但摇动动画不起作用。这是我所拥有的:

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

我尝试在- (void)shake...中放置一个NSLog,并得到了一个输出。所以我的代码一定有问题,为什么alertView不抖动。

您可以在iOS中使用UIKit Dynamics制作动画效果。这是一个教程,它使用UIKit Dynamics进行UIAlertView抖动动画。有关更多信息,您也可以参考本教程,该教程是 swift。

我创造了一种错觉,使警报视图看起来像在摇晃。前提是您必须设置有关警报视图(包括深色背景屏幕)应摇晃多少的参数。

由于您无法使用UIAlertView的框架进行更改,因此请使用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,而且可用的开源库也很少。

最新更新