CALayer的动画回复(CALayer animation reverts)

2019-10-17 23:09发布

我一直在努力做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

Answer 1:

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

这些都不是解决办法。

CGPoint newLoc = pointerLayer.position;

这将没有任何好处; 添加动画实际上并没有改变位置。 动画发生。

我不能完全肯定你是什么之后,但它可以帮助你阅读我的Core Animation的解释。 动画只是一种假象; 这是与“表现层”的游戏。 它是由来移动真正的层到它会是动画之后, 添加将出现让它动有动画。

我为确保您的核心动漫作品,你想要去得到你一个公式化的方法:

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



Answer 2:

我似乎无法重现此 - 授予我用一些测试数据:

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

可能在阵列中的点之一可能是首发位置?



文章来源: CALayer animation reverts