几天来我一直在尝试这样做。我有一组 CGPoints 可以形成曲线或直线(手绘)。我正在使用 CALayer 并希望以平滑的方式(如圆形路径)对其进行动画处理。到目前为止,这是我尝试过的。我在一些链接上找到了下面的代码,但它的动画效果并不流畅。需要一些帮助提前谢谢
curvedPath = CGPathCreateMutable();
const int TOTAL_POINTS = 3;
int horizontalWiggle = 15;
CGPoint viewOrigin = [[arrOfLocations objectAtIndex:0] CGPointValue];
CGPoint endPoint = [[arrOfLocations lastObject] CGPointValue];
int stepChangeX = (endPoint.x - viewOrigin.x) / TOTAL_POINTS;
int stepChangeY = (endPoint.y - viewOrigin.y) / TOTAL_POINTS;
for(int i = 0; i < TOTAL_POINTS; i++) {
int startX = (int)(viewOrigin.x + i * stepChangeX);
int startY = (int)(viewOrigin.y + i * stepChangeY);
int endX = (int)(viewOrigin.x + (i+1) * stepChangeX);
int endY = (int)(viewOrigin.y + (i+1) * stepChangeY);
int cpX1 = (int)(viewOrigin.x + (i+0.25) * stepChangeX);
if((i+1)%2) {
cpX1 -= horizontalWiggle;
} else {
cpX1 += horizontalWiggle;
}
int cpY1 = (int)(viewOrigin.y + (i+0.25) * stepChangeY);
int cpX2 = (int)(viewOrigin.x + (i+0.75) * stepChangeX);
if((i+1)%2) {
cpX2 -= horizontalWiggle;
} else {
cpX2 += horizontalWiggle;
}
int cpY2 = (int)(viewOrigin.y + (i+0.75) * stepChangeY);
CGPathMoveToPoint(curvedPath, NULL, startX, startY);
CGPathAddCurveToPoint(curvedPath, NULL, cpX1, cpY1, cpX2, cpY2, endX, endY);
并为图层设置动画。我用过
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
keyFrameAnimation.delegate = self;
// [keyFrameAnimation setCalculationMode:kCAAnimationPaced];
keyFrameAnimation.duration = 1;
keyFrameAnimation.removedOnCompletion = NO;
[keyFrameAnimation setFillMode:kCAFillModeForwards];
keyFrameAnimation.path = curvedPath2;
arrayOfAnimations addObject:[NSDictionary dictionaryWithObjectsAndKeys:basicAni,@"name",[NSNumber numberWithInt:1],@"toValue",@"appear",@"type", nil] ];
[self animationHandler];
- (void)animationHandler{
isAnimationInProgress = YES;
NSDictionary * dict = [arrayOfAnimations objectAtIndex:0];
NSString * idd = [dict objectForKey:@"type"];
if ([idd isEqualToString:@"path"]) {
CGPoint p = [[dict valueForKey:@"toValue"]CGPointValue];
CAKeyframeAnimation *keyF = [dict objectForKey:@"name"];
keyF.delegate = self;
pointerLayer.position = p;
[pointerLayer addAnimation:keyF forKey:@"path"];
}
else
{
int p = [[dict valueForKey:@"toValue"]intValue];
pointerLayer.opacity = p;
CABasicAnimation *basic = [dict objectForKey:@"name"];
basic.delegate = self;
[pointerLayer addAnimation:basic forKey:@"opacity"];
}
}