在图层中的曲线中设置动画

时间:2013-05-15 08:25:54

标签: ios objective-c calayer caanimation

我一直试图这样做几天。我有一系列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);

和Animate 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"];

    }


}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

K我发现我在哪里错了.....我在某种程度上实现了我所寻找的......我创造了错误的道路。我从下面的代码创建了路径。如果数组中的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);