如何做文本字段抖动效果?

How to do text field shake effect?

我正在尝试模仿 Mac OSX 中登录时的效果:当您输入错误的密码时,文本字段开始出现漂亮的摇动效果。我试过了,但我不满意。

-(void)shakeText:(UITextField*)textField
{
     [UIView animateWithDuration:0.25
                      delay:0.0
                    options:UIViewAnimationOptionAutoreverse
                 animations:^{textField.transform =CGAffineTransformMakeTranslation(5, 0);}
                 completion:^(BOOL finished) {
                     [UIView animateWithDuration:0.5
                                           delay:0.0
                                         options:UIViewAnimationOptionAutoreverse
                                      animations:^{textField.transform = CGAffineTransformMakeTranslation(5, 0);}
                                      completion:nil];
                 }];
}

iOS 中是否有函数可以正确执行此操作?

请尝试以下代码:

// Change the 10 & -10 values as you wish
CGAffineTransform leftWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-10.0));
CGAffineTransform rightWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(10.0));

textField.transform = leftWobble;

[UIView animateWithDuration:0.25
        delay:0
        options:(UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse) animations:^{

        [UIView setAnimationRepeatCount:6]; // Change this value as you want

        textField.transform = rightWobble;

} completion:^(BOOL finished){

        textField.transform = CGAffineTransformIdentity;

}];