围绕它旋转CAShapeLayer而不移动位置

时间:2015-12-02 11:49:19

标签: ios objective-c

我想围绕它的中心点旋转一个带有物镜c的CAShapeLayer而不移动它。 CAShapeLayer包含UT的UIBezierPath点。我无法旋转CAShapeLayer因为我不知道怎么做。请告诉我tp如何围绕它旋转而不移动它。

1 个答案:

答案 0 :(得分: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类型。