【问题标题】:Create a shake animation for a UIAlertView为 UIAlertView 创建抖动动画
【发布时间】:2015-06-12 08:36:54
【问题描述】:

我正在尝试摇晃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];
                                  }];
}

我尝试将NSLog 放入- (void)shake... 并得到输出。所以我的代码一定有问题,为什么alertView 没有晃动。

【问题讨论】:

  • 再一次,你必须继承你的 UIView/AlertView 或者使用这个 github.com/lukestringer90/ShakingAlertView 库
  • @user3693546 我试过了,但是没用
  • 你是指整个屏幕抖动还是只是 AlertView?
  • 只是alertView
  • @Jessica 查看答案 Jessy。

标签: ios objective-c animation uialertview


【解决方案1】:

您可以在 iOS 中使用 UIKit Dynamics 制作动画效果。这是一个tutorial,它使用UIKit Dynamics 制作UIAlertView 摇动动画。有关更多信息,您可以参考这个 tutorial ,它也在 swift 中。

【讨论】:

    【解决方案2】:

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

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

    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,而且可用的开源库也很少。

    【讨论】:

    • 你的意思是我应该从头开始,(从 UIView)?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    相关资源
    最近更新 更多