2016-08-05 58 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]; 

}];