CALayer动画还原

时间:2013-04-04 13:16:12

标签: ios calayer caanimation

我一直在尝试CALayer动画。但是在动画之后它会回复。我找到了一些像removedOnCompleteion = YESfill mode = kCAFillModesForwards这样的解决方案,但没有人帮我。这是代码。 thx需要一点帮助。

  void(^ myblock)(NSDictionary *) = ^(NSDictionary *dict){

        NSMutableArray *arrOfPt=       [dict objectForKey:@"Path"];

        CGPoint p1 = [[arrOfPt objectAtIndex:0] CGPointValue];

        CGPoint cp1 = [[arrOfPt objectAtIndex:1] CGPointValue];

//        CGPoint cp1 = [[arrOfPt objectAtIndex:1] CGPointValue];
        CGPoint cp2 = [[arrOfPt objectAtIndex:2] CGPointValue];
        CGPoint p2 = [[arrOfPt objectAtIndex:3] CGPointValue];

        NSMutableArray *arrayTime =  [dict objectForKey:@"Time"];
        CGFloat time = [[arrayTime objectAtIndex:0] floatValue];

        pointerLayer.opacity = 1;
//        NSArray *oneObjPt = [NSArray arrayWithObjects:[arrOfPt objectAtIndex:0],[arrOfPt lastObject],nil];
        //         NSArray *oneObjTime = [NSArray arrayWithObjects:[arrayTime objectAtIndex:0],[arrayTime lastObject],nil];
//        NSArray *oneObjTime = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:1.0],nil];
        CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

//        [keyFrameAnimation setCalculationMode:kCAAnimationPaced];
        keyFrameAnimation.duration = 1;
        keyFrameAnimation.removedOnCompletion = NO;
        [keyFrameAnimation setFillMode:kCAFillModeForwards];

        CGMutablePathRef fpath = CGPathCreateMutable();

        CGPathMoveToPoint(fpath, NULL, p1.x, p1.y);
        CGPathAddCurveToPoint(fpath, NULL, cp1.x, cp1.y, cp2.x, cp2.y, p2.x, p2.y);
        keyFrameAnimation.path = fpath;

        [pointerLayer addAnimation:keyFrameAnimation forKey:@"pos"];
        CGPoint newLoc = pointerLayer.position;// this is just to verify the layer's location

2 个答案:

答案 0 :(得分:1)

  

我找到了一些解决方案,例如removedOnCompleteion = YES和填充模式= kCAFillModesForwards

那些不是解决方案。

  

CGPoint newLoc = pointerLayer.position;

这不会有任何好处;添加动画实际上并不会改变位置。动画稍后发生

我不完全确定你的目标是什么,但它可能会帮助你阅读我对Core Animation的解释。动画只是一种幻觉;这是一个用“表示层”玩的游戏。 真实的层移动到动画之后的位置,以添加将出现的动画让它移动到那里。

我提供了一种公式化的方法,以确保你的核心动画能够让你到达目的地:

http://www.apeth.com/iOSBook/ch17.html#_using_a_cabasicanimation

答案 1 :(得分:0)

我似乎无法重新创建这个 - 我使用了一些测试数据:

CGPoint cp1 = CGPointMake(0, 0);
CGPoint cp2 = CGPointMake(100, 100);
CGPoint cp3 = CGPointMake(85, 150);
CGPoint cp4 = CGPointMake(50, 50);
CGFloat time = 2.0f;
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
keyFrameAnimation.duration = time;
keyFrameAnimation.removedOnCompletion = NO;
[keyFrameAnimation setFillMode:kCAFillModeForwards];

CGMutablePathRef fpath = CGPathCreateMutable();

CGPathMoveToPoint(fpath, NULL, cp1.x, cp1.y);
CGPathAddCurveToPoint(fpath, NULL, cp2.x, cp2.y, cp3.x, cp3.y, cp4.x, cp4.y);
keyFrameAnimation.path = fpath;
[testView.layer addAnimation:keyFrameAnimation forKey:@"pos"];
CFRelease(fpath);

输出:

http://glassofcocoa.com/code/AnimationTest.mp4

阵列中的一个点可能是起始位置?