Iphone 当文本字段为空时,如果用户按下提交按钮,则晃动UIAlertView

Iphone 当文本字段为空时,如果用户按下提交按钮,则晃动UIAlertView,iphone,ios,ipad,uialertview,Iphone,Ios,Ipad,Uialertview,如果用户在文本字段中未输入任何数据的情况下按下提交按钮,我想晃动UIAlertView。在iOS中可能吗 在这里,当验证失败时,代码调用此方法 - (void)animateView { CAKeyframeAnimation *animation; animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"]; animation.duration = 2.0;

如果用户在文本字段中未输入任何数据的情况下按下提交按钮,我想晃动
UIAlertView
。在iOS中可能吗


在这里,当验证失败时,代码调用此方法

- (void)animateView 
{
  CAKeyframeAnimation *animation;
        animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
        animation.duration = 2.0;
        animation.cumulative = NO;
        animation.repeatCount = MAXFLOAT;
        animation.values = [NSArray arrayWithObjects:
                            [NSNumber numberWithFloat: 0.0], 
                            [NSNumber numberWithFloat: DEGREES_TO_RADIANS(-4.0)], 
                            [NSNumber numberWithFloat: 0.0],
                            [NSNumber numberWithFloat: DEGREES_TO_RADIANS(4.0)],
                            [NSNumber numberWithFloat: 0.0], nil];
        animation.fillMode = kCAFillModeBoth;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        animation.removedOnCompletion = NO;
        [[alertView layer] addAnimation:animation forKey:@"effect"];
}
无论何时要停止动画,都可以调用此动画

- (void)stopAnimatiomn
{
    [[alertView layer] removeAnimationForKey:@"effect"];
}

首先添加内部头文件add

int direction;
int shakes;
用于防止UIAlertView解雇。参考链接。另请参阅链接

使用UIAlertView委托:

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  if (buttonIndex == 0)
    //do here something
  else if (buttonIndex == 1){
    if(txtField.text.length == 0 || txtField1.text.length == 0) //check your two textflied has value
    {
      direction = 1;
      shakes = 0;
    }
  }
}
添加此方法:

-(void)shake:(UIAlertView *)theOneYouWannaShake
{
  [UIView animateWithDuration:0.03 animations:^
                                  {
                                    theOneYouWannaShake.transform = CGAffineTransformMakeTranslation(5*direction, 0);
                                  } 
                                  completion:^(BOOL finished) 
                                  {
                                    if(shakes >= 10)
                                    {
                                      theOneYouWannaShake.transform = CGAffineTransformIdentity;
                                      return;
                                    }
                                    shakes++;
                                    direction = direction * -1;
                                    [self shake:theOneYouWannaShake];
                                  }];
}
Reference是实现此功能的UIAlertView子类


免责声明:我是ShakingAlertView的开发者

我喜欢这种方法,它很干净。不过,您的代码格式要更好/更一致+1.我试了一下,但我的警觉颤抖了一下,然后消失了。我不想让它消失