如何做文字场震动效果?

时间:2015-04-12 09:56:35

标签: ios animation uiviewanimation

我试图模仿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中是否有正确的功能?

1 个答案:

答案 0 :(得分:2)

请尝试以下代码:

// 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;

}];