gpt4 book ai didi

ios - CAKeyframeAnimation 停止 CGPathAddArc 的某些角度值

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:23:38 25 4
gpt4 key购买 nike

我正在尝试制作动画,其中一个圆圈围绕 View 的中心旋转。

我希望圆圈出现在点击手势的位置下方,因此我计算了 View 中心与圆圈之间的距离和角度。

然后我在 CGPathAddArc 中为动画路径设置这些值。

这似乎工作得很好,但偶尔,在执行完整的旋转后,圆圈会停一会儿,然后重新开始。通过尝试,我发现这一定与 CGPathAddArc 函数的开始和结束角度有关,因为将标准值设置为 -M_PI 到 M_PI 不会导致问题。

我的代码是:

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.fillMode = kCAFillModeRemoved;
pathAnimation.removedOnCompletion = YES;
pathAnimation.duration = 4.0;
pathAnimation.repeatCount = HUGE_VALF;

CGMutablePathRef curvedPath = CGPathCreateMutable();



//get the angle and dinstance of location in relation to center of view (mathematics)
CGFloat angle = atan2f(location.y -self.view.center.y, location.x - self.view.center.x);
CGFloat distance = hypotf(location.x - self.view.center.x, location.y - self.view.center.y);
NSLog(@"angle: %f", angle);
CGPathAddArc(curvedPath, NULL, self.view.center.x, self.view.center.y, distance, angle, angle + 2 * M_PI, NO);

//Now we have the path, we tell the animation we want to use this path - then we release the path
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);

//Add the animation to the circleView - once you add the animation to the layer, the animation starts
[self.view addSubview:circle];
[circle.layer addAnimation:pathAnimation forKey:@"moveTheSquare"];

location 保存了触摸事件的坐标,圆形对象是在这段代码之前创建的。任何帮助将不胜感激。

最佳答案

添加这个会使动画均匀平滑:

pathAnimation.calculationMode = kCAAnimationPaced;

刚刚测试过。

顺便说一句,计算不错。

希望这对您有所帮助。

关于ios - CAKeyframeAnimation 停止 CGPathAddArc 的某些角度值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23330329/

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