如果用户在textFields为空时按“提交”按钮,则摇动UIAlertView

时间:2012-10-31 08:17:50

标签: iphone ios ipad uialertview

如果用户按下提交按钮而不在textFields中输入任何数据,我想摇动UIAlertView。在iOS中可以吗?

enter image description here

3 个答案:

答案 0 :(得分:2)

首先添加内部头文件add

int direction;
int shakes;

用于防止UIAlertView被解雇。请参阅keep-uialertview-displayed链接。另请参阅prevent-alertview-dismissal链接。

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

参考more here about animation

答案 1 :(得分:1)

ShakingAlertView是一个实现此功能的UIAlertView子类。

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

答案 2 :(得分:0)

此处代码调用此方法验证失败

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