设置 CGAffineTransform 的起点

发布于 2024-10-13 19:14:01 字数 655 浏览 3 评论 0原文

我使用遵循 CGPathAddEllipseInRect 的 CAKeyframeAnimation 沿着圆圈对 UIView 进行动画处理。但是,无论视图最初位于哪个框架,视图似乎总是从同一个位置开始。是否有某种方法可以调整视图在路径上的起始位置?

谢谢!

CAKeyframeAnimation *myAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
myAnimation.fillMode = kCAFillModeForwards;
myAnimation.repeatCount = 5;
myAnimation.calculationMode = kCAAnimationPaced;
myAnimation.duration = 10.0;

CGMutablePathRef animationPath = CGPathCreateMutable();

CGPathAddEllipseInRect(animationPath, NULL, rect);

myAnimation.path = animationPath;
CGPathRelease(animationPath);

[view.layer addAnimation:myAnimation forKey:@"changeViewLocation"];

I am animating a UIView along a circle using a CAKeyframeAnimation that follows a CGPathAddEllipseInRect. However, the view always seems to start in the same place regardless of the frame it is originally positioned in. Is there some way to adjust the starting position of the view on the path?

Thanks!

CAKeyframeAnimation *myAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
myAnimation.fillMode = kCAFillModeForwards;
myAnimation.repeatCount = 5;
myAnimation.calculationMode = kCAAnimationPaced;
myAnimation.duration = 10.0;

CGMutablePathRef animationPath = CGPathCreateMutable();

CGPathAddEllipseInRect(animationPath, NULL, rect);

myAnimation.path = animationPath;
CGPathRelease(animationPath);

[view.layer addAnimation:myAnimation forKey:@"changeViewLocation"];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦萦几度 2024-10-20 19:14:01

我认为不可能为 CGPathAddEllipseInRect 设置起点。 (至少我没有成功)

而不是使用:CGPathAddEllipseInRect
您应该使用:CGPathAddArc! 例如:

CAKeyframeAnimation *myAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
myAnimation.fillMode = kCAFillModeForwards;
myAnimation.repeatCount = 5;
myAnimation.calculationMode = kCAAnimationPaced;
myAnimation.duration = 10.0;

CGMutablePathRef animationPath = CGPathCreateMutable();

NSInteger startVal = M_PI / 2;

//seventh value (end point) must be always 2*M_PI bigger for a full circle rotation
CCGPathAddArc(animationPath, NULL, 100, 100, 100,  startVal, startVal + 2*M_PI, NO); 

myAnimation.path = animationPath;
CGPathRelease(animationPath);

[view.layer addAnimation:myAnimation forKey:@"changeViewLocation"];

将从底部开始顺时针旋转整圈。更改 startVal 值可更改旋转起始位置。 (完整旋转为 2*M_PI)。

I don't think it's possible to set a start point for CGPathAddEllipseInRect. (At least i wasn't successful)

instead of using: CGPathAddEllipseInRect,
You should use: CGPathAddArc! For example:

CAKeyframeAnimation *myAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
myAnimation.fillMode = kCAFillModeForwards;
myAnimation.repeatCount = 5;
myAnimation.calculationMode = kCAAnimationPaced;
myAnimation.duration = 10.0;

CGMutablePathRef animationPath = CGPathCreateMutable();

NSInteger startVal = M_PI / 2;

//seventh value (end point) must be always 2*M_PI bigger for a full circle rotation
CCGPathAddArc(animationPath, NULL, 100, 100, 100,  startVal, startVal + 2*M_PI, NO); 

myAnimation.path = animationPath;
CGPathRelease(animationPath);

[view.layer addAnimation:myAnimation forKey:@"changeViewLocation"];

will rotate full circle starting from bottom - clockwise. Change startVal value to change rotation start position. (full rotation is 2*M_PI).

跨年 2024-10-20 19:14:01

以前的答案是正确的并且有效,但应该有

CGFloat startVal = M_PI / 2;

而不是

NSInteger startVal = M_PI / 2;

否则,M_PI 将四舍五入为整数,并且您将无法获得准确的角度。
PS 声誉较低,无法评论之前的答案,抱歉。

Previous answer is correct and works, but there should be

CGFloat startVal = M_PI / 2;

instead of

NSInteger startVal = M_PI / 2;

Otherwise, M_PI will be rounded to integer and you won't be able to achieve accurate angle.
P.S. has low reputation to comment the previous answer, sorry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文