我正在尝试制作一个关键帧动画,让图像视图围绕一个圆圈飞行。我CGPathAddCurveToPoint
用来制作关键帧点。问题是图像视图总是使用最短(线性)路线。
我想要某种函数来为给定的半径构建圆形路径。
非常感谢您的帮助。
我正在尝试制作一个关键帧动画,让图像视图围绕一个圆圈飞行。我CGPathAddCurveToPoint
用来制作关键帧点。问题是图像视图总是使用最短(线性)路线。
我想要某种函数来为给定的半径构建圆形路径。
非常感谢您的帮助。
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, startPoint.x, startPoint.y);
CGPathAddQuadCurveToPoint(path, NULL,middlePoint.x, middlePoint.y,endPoint.x,endPoint.y);
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.removedOnCompletion = NO;
pathAnimation.path = path;
[pathAnimation setCalculationMode:kCAAnimationCubic];
[pathAnimation setFillMode:kCAFillModeForwards];
pathAnimation.duration = 2.0;
[viewTobemoved.layer addAnimation:pathAnimation forKey:nil];
这个动画只使用了一个贝塞尔点,从起点到终点做一个软圆。如果你想通过定义圆的半径来精确定义曲线,可以参考以下链接
http://blog.shoguniphicus.com/2011/05/19/keyframe-animation-ios-cakeyframeanimation-objective-c/
这会奏效吗?
CAKeyframeAnimation* circlePathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"center"];
CGMutablePathRef circularPath = CGPathCreateMutable();
CGRect pathRect = ...; // some rect you define. A circle has equal width/height; radius is half the values you specify here
CGPathAddEllipseInRect(circularPath, NULL, pathRect);
spinAnimation.path = circularPath;
CGPathRelease(circularPath);
您会注意到动画的关键路径是您希望绕圈飞行的 UIView 的中心属性。