gpt4 book ai didi

ios - 围绕其中心旋转CAShapeLayer,而不移动位置

转载 作者:行者123 更新时间:2023-12-01 16:28:10 24 4
gpt4 key购买 nike

我想围绕目标中心旋转带有 objective-c 的CAShapeLayer,而无需四处移动。 CAShapeLayer包含矩形的UIBezierPath点。我无法旋转CAShapeLayer,因为我不知道如何操作。请告诉我tp如何绕其中心旋转而不移动其位置。

最佳答案

这是执行此操作的一些代码:

//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类型,同样的方法也适用。

关于ios - 围绕其中心旋转CAShapeLayer,而不移动位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34042014/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com