2013-08-22 71 views
0

即時通訊尋求旋轉一個UIImageView,我發現了一些有益的代碼IOS需要旋轉圖像,並保持它在那裏,動畫

- (void) runSpinAnimationOnView:(UIView*)view duration:(CGFloat)duration rotations: (CGFloat)rotations repeat:(float)repeat; 
{ 
    CABasicAnimation* rotationAnimation; 
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ]; 
    rotationAnimation.duration = duration; 
    rotationAnimation.cumulative = YES; 
    rotationAnimation.repeatCount = repeat; 

    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 

} 

的問題是,圖像翻轉回來時,在動畫完成,即時通訊希望旋轉它並保持在那裏。我需要翻轉實際圖像還是可以翻轉ImageView?而我將如何做到這一點?

+0

這應該這樣做'rotationAnimation.removedOnCompletion = NO;' – BooRanger

+0

不起作用,仍然翻轉 –

+0

您可以使用' dispatch_after'塊並將'view.transform.rotation.z'分發到所需的值。有點黑客,但它應該工作。 – Majster

回答

-1

我更喜歡用CoreAnimation覆蓋我的所有基地。設置動畫前的最終值,設置填充模式,並設置不刪除。

- (void) runSpinAnimationOnView:(UIView*)view duration:(CGFloat)duration rotations:   (CGFloat)rotations repeat:(float)repeat; 
{ 
    NSString * animationKeyPath = @"transform.rotation.z"; 
    CGFloat finalRotation = M_PI * 2.0f * rotations; 
    [view.layer setValue:@(finalRotation) forKey:animationKeyPath]; 

    CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:animationKeyPath]; 
    rotationAnimation.fromValue = @0.0f; 
    rotationAnimation.toValue = @(finalRotation); 
    rotationAnimation.fillMode = kCAFillModeBoth; 
    rotationAnimation.removedOnCompletion = NO; 
    rotationAnimation.duration = duration; 
    rotationAnimation.cumulative = YES; 
    rotationAnimation.repeatCount = repeat; 

    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 
} 
0

爲什麼不使用標準UIView animation? 如果你真的需要repeatCount,那麼我建議你使用舊的風格:

[UIView beginAnimations] 
[UIView setAnimationRepeatCount:repeatCount]; 
[UIView setAnimationDuration:duration]; 
yourView.transform = CGAffineTransformMakeRotation(degrees * M_PI); 
[UIView commitAnimations]; 

否則,您可以使用非常優選的新風格:

[UIView animateWithDuration:duration animations:^{ 
    yourView.transform = CGAffineTransformMakeRotation(angle); 
} completion:^(BOOL finished) { 
    // your completion code here 
}] 

如果需要其他選項,如自動翻轉,允許用戶交互或重複使用[UIView animateWithDuration:delay:options:animations:completion:];