2014-04-01 169 views
0

我想執行一些旋轉動畫。現在我這樣做:ios旋轉動畫與CGAffineTransformRotate

#define degreesToRadians(x) (M_PI * x/180.0) 

[self.crossButton setTransform:CGAffineTransformRotate(self.crossButton.transform, degreesToRadians(-rotationDegree))]; 

但當我通過例如360度nothong發生。當我超過180的價值時,它開始不好。你知道我做錯了什麼嗎?

回答

1

你的問題是'setTransformation'與矩陣旋轉一起工作。出於這個原因,你將永遠得到最終結果的最短路線。當你通過360度旋轉時,你的物體在變形後將是相同的。出於這個原因,轉型將無所作爲,因爲它已經是它應該結束的地方。對於180到360度之間的值,您的旋轉將會「倒退」,因爲再次。輪換使用「最短」路徑來達到最終結果。

你可以試試這個代碼:

UIView* toRotate = VIEW_TO_ROTATE; 
CGFloat degreesToRotate = DEGREES; 
CGFloat animationTime = TOTAL_ANIMATION_TIME; 


NSInteger intervals = ((int)degreesToRotate)/179.9; 
CGFloat rest = degreesToRotate-(intervals*179.9); 

CGFloat radInterval = degreesToRotate>=0?179.9:-179.9; 
CGFloat radRest = (M_PI * rest/180.0); 

CGFloat intervalTime = (1-(radRest/M_PI/2))/intervals; 
CGFloat restTime = (radRest/M_PI/2)/intervals; 

[UIView animateKeyframesWithDuration:animationTime 
           delay:0.0f 
          options:UIViewKeyframeAnimationOptionCalculationModeLinear 
          animations: 
^{ 
    for (int i=0; i<intervals; i++) { 
     [UIView addKeyframeWithRelativeStartTime:intervalTime*i relativeDuration:intervalTime animations:^{ 
      toRotate.transform = CGAffineTransformConcat(toRotate.transform, CGAffineTransformMakeRotation(radInterval)); 
     }]; 
    } 
    [UIView addKeyframeWithRelativeStartTime:intervalTime*intervals relativeDuration:restTime animations:^{ 
     toRotate.transform = CGAffineTransformConcat(toRotate.transform, CGAffineTransformMakeRotation(radRest)); 
    }]; 
} completion:^(BOOL finished) { 

}]; 

請務必將VIEW_TO_ROTATEDEGREESTOTAL_ANIMATION_TIME與價值觀,你需要!