0

几天来我一直在尝试这样做。我有一组 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"];

    }


}
4

2 回答 2

0

尝试使用... 您可以为其属性CAShapeLayer设置动画。path

于 2013-05-15T08:49:13.220 回答
0

我发现我在哪里弄错了......我在某种程度上实现了我正在寻找的东西......我创造了错误的路径。我从下面的代码创建了路径。如果数组中的 CGPoints 形成圆形,它会以圆形动画图层......但它现在有一些混蛋。

CGPoint p1 = [[arrOfLocations objectAtIndex:i] CGPointValue];
        CGPoint p2 = [[arrOfLocations objectAtIndex:i+1] CGPointValue];

        CGFloat x = p2.x-p1.x;
        CGFloat y = p2.y-p1.y;

        CGPathMoveToPoint(curvedPath2, NULL, p1.x, p1.y);
        CGPathAddLineToPoint(curvedPath2, NULL, p2.x, p2.y);
于 2013-05-15T12:07:07.810 回答