下面的代码来自这个SO问题:UIView shake animation。如何使用animateWithDuration无限期地重复晃动动画或X次?
这个动摇动画是完美的,除了一次迭代后停止。你如何无限期地重复动画,或者你如何使它重复X次?
还有其他的答案重复UIView动画,建议使用CAKeyframeAnimation
或CABasicAnimation
,但这个问题是不同的。我们的目标是重现这种精确的动画效果,从usingSpringWithDamping
和initialSpringVelocity.
和initialSpringVelocity.
使用自动反向和重复不会重现所需的效果,因为初始翻译在动画块之外。
view.transform = CGAffineTransformMakeTranslation(20, 0);
[UIView animateWithDuration:0.4 delay:0.0 usingSpringWithDamping:0.2 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
view.transform = CGAffineTransformIdentity;
} completion:nil];