3

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

在此处输入图像描述

4

3 回答 3

2

首先添加里面的头文件添加

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

请参阅此处有关动画的更多信息

于 2012-10-31T08:25:31.647 回答
1

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

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

于 2013-03-04T11:45:34.387 回答
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"];
}
于 2012-10-31T08:25:16.467 回答