2015-12-02 64 views
0

我想旋轉一個CAShapeLayer,圍繞它的中心點旋轉一個CAShapeLayer而不移動它。 CAShapeLayer包含UIBezierPath rect點。我無法旋轉CAShapeLayer,因爲我不知道如何。請告訴我如何繞中心旋轉而不移動它的位置。圍繞它旋轉CAShapeLayer而無需移動位置

+1

[iOS - 使用兩個不同的UIButtons左右旋轉圖層]的可能副本(http://stackoverflow.com/questions/21170772/ios-rotate-a-layer-right-and-left-using-two -different-uibuttons) – ShahiM

+0

我看到你的問題。你能告訴我們更多關於你作爲BezierPath繪製的形狀嗎? – Kujey

+0

我正在繪製bezierPathWithRect –

回答

0

下面是一些代碼,做的是:

//Create a CABasicAnimation object to manage our rotation. 
CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform"]; 
totalAnimationTime = rotation_count; 

rotation.duration = totalAnimationTime; 

//Start the animation at the previous value of angle 
rotation.fromValue = @(angle); 

//Add change (which will be a change of +/- 2pi*rotation_count 
angle += change; 

//Set the ending value of the rotation to the new angle. 
rotation.toValue = @(angle); 

//Have the rotation use linear timing. 
rotation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 

/* 
This is the magic bit. We add a CAValueFunction that tells the CAAnimation we are modifying 
the transform's rotation around the Z axis. 
Without this, we would supply a transform as the fromValue and toValue, and for rotations 
    > a half-turn, we could not control the rotation direction. 

By using a value function, we can specify arbitrary rotation amounts and directions, and even 
Rotations greater than 360 degrees. 
*/ 

rotation.valueFunction = [CAValueFunction functionWithName: kCAValueFunctionRotateZ]; 


/* 
    Set the layer's transform to it's final state before submitting the animation, so it is in it's 
    final state once the animation completes. 
*/ 
imageViewToAnimate.layer.transform = CATransform3DRotate(imageViewToAnimate.layer.transform, angle, 0, 0, 1.0); 

//Now actually add the animation to the layer. 
[imageViewToAnimate.layer addAnimation:rotation forKey:@"transform.rotation.z"]; 

(該代碼取(和簡化的)從我的GitHub項目KeyframeViewAnimations

在我的項目,我旋轉UIImageView層,但同樣的方法適用於任何類型的CALayer

+0

不圍繞對象中心旋轉,對象移動位置 –