2017-04-06 209 views
0

我需要在180度的x軸上旋轉我的視圖。旋轉完成,但當它開始旋轉時,它會摺疊我的半視圖。這是我所做的任何幫助的代碼。CABasicAnimation在x軸上旋轉視圖

 CABasicAnimation* rotationAnimation; 
     rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"]; 
     rotationAnimation.fromValue = [NSNumber numberWithFloat:0]; 
     rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0]; 
     rotationAnimation.duration = 2.0; 
     rotationAnimation.cumulative = YES; 
     rotationAnimation.repeatCount = 0; 
     [_view_topShowDate.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 
+0

您可以發佈摺疊視圖的圖像嗎? – shallowThought

+0

[使用CABasicAnimation多次旋轉UIImageView]的可能重複(http://stackoverflow.com/questions/17426802/using-cabasicanimation-to-rotate-a-uiimageview-more-than-once) –

回答

0

您必須在viewDidLoad或動畫開始之前的某個位置設置定位點。默認情況下,它將anchorPoint作爲(0.5,0.5),因此您可以看到來自中心的旋轉。

_viewToRotate.layer.anchorPoint = CGPointMake(0.5, 1.0); 

然後調用基本的動畫方法。

瞭解更多關於http://ronnqvi.st/about-the-anchorpoint/

當你改變錨點,它移動層的位置。 要重新定位它,請使用以下內容

-(void)setNewAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view 
{ 
    CGPoint newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, view.bounds.size.height * anchorPoint.y); 
    CGPoint oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, view.bounds.size.height * view.layer.anchorPoint.y); 

    newPoint = CGPointApplyAffineTransform(newPoint, view.transform); 
    oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform); 

    CGPoint position = view.layer.position; 

    position.x -= oldPoint.x; 
    position.x += newPoint.x; 

    position.y -= oldPoint.y; 
    position.y += newPoint.y; 

    view.layer.position = position; 
    view.layer.anchorPoint = anchorPoint; 
}