2016-08-05 22 views
0

下面的代码来自这个SO问题:UIView shake animation如何使用animateWithDuration无限期地重复晃动动画或X次?

这个动摇动画是完美的,除了一次迭代后停止。你如何无限期地重复动画,或者你如何使它重复X次?

还有其他的答案重复UIView动画,建议使用CAKeyframeAnimationCABasicAnimation,但这个问题是不同的。我们的目标是重现这种精确的动画效果,从usingSpringWithDampinginitialSpringVelocity.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]; 

回答

0

如果您需要完全相同的代码几次比把这些代码的方法,做递归,这样的事情:

- (void)animateCount:(NSInteger)count { 
    if (count == 0) { 
     return; 
    } 

    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:^{ 
     count--; 
     [self animateCount:count]; 
    }]; 
} 
0

使用(UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat)在选择重复它无限期

[UIView animateWithDuration:0.4 delay:0.0 usingSpringWithDamping:0.2 initialSpringVelocity:1.0 options:(UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut) animations:^{ 

    someView.frame = someFrame1; 

} completion:^(BOOL finished) { 

    [UIView animateWithDuration:0.5f animations:^{ 

     someView.frame = someFrame2; 

    } completion:nil]; 

}];