私はこれを数日間やろうとしています。曲線または直線 (フリーハンド) を形成できる 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);
そして、Layer.Iをアニメーション化するために使用しました
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"];
}
}