我希望進行動畫沿弧線的視圖,作爲示出的圖像中從位置1到位置2動畫一個UIView沿弧線
什麼實際發生的是,視圖的動畫描述了一種全圓而不是弧。我的問題是:
- 我應該使用CGPathAddArc或CGPathAddArcToPoint?
- 我是否需要使用CGPathMoveToPoint來描述視圖的初始位置?
當我單步執行代碼時,CGPoints值與我預期的一樣,正如角度一樣。
我用下面的函數來獲得
CGPoint pointOnCircle(CGPoint centre, float radius, float angle)
{
float x = centre.x + radius * cosf(angle);
float y = centre.y + radius * sinf(angle);
return CGPointMake(x, y);
}
我使用下面的代碼及其變體,但至今我還沒有設法破解它的圓CGPoints。
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = duration;
pathAnimation.repeatCount = 1;
CGPoint viewCenter = dynamicView.objectCenter;
CGMutablePathRef arcPath = CGPathCreateMutable();
// CGPathMoveToPoint(arcPath, NULL, viewCenter.x, viewCenter.y);
//work out the half way point
float halfwayAngle = dynamicView.angle + (0.5f * self.angleIncrement);
float fullwayAngle = dynamicView.angle + self.angleIncrement;
CGPoint halfwayPoint = pointOnCircle(self.center, self.radius, halfwayAngle);
CGPoint fullwayPoint = pointOnCircle(self.center, self.radius, fullwayAngle);
CGPathAddArc(arcPath, NULL, halfwayPoint.x, halfwayPoint.y, self.radius, dynamicView.angle, fullwayAngle, clockwise);
// CGPathAddArcToPoint(arcPath, NULL, viewCenter.x, viewCenter.y, fullwayPoint.x, fullwayPoint.y, self.radius);
pathAnimation.path = arcPath;
CGPathRelease(arcPath);
[dynamicView.layer addAnimation:pathAnimation forKey:@"arc"];
'MoveToPoint'到第一個位置,然後'AddArcToPoint'到第二個位置。 – Kevin