ios – 在动画过程中无法获取CALayer的当前位置

我试图实现一个动画,当你按住一个按钮时,它会动画化一个块,当你释放时,它会将其动画回到原来的位置,但无论如何,我无法获得动画块的当前位置.这是我的代码:
-(IBAction)moveDown:(id)sender{

   CGRect position = [[container.layer presentationLayer] frame];

    [movePath moveToPoint:CGPointMake(container.frame.origin.x,position.y)];

    [movePath addLineToPoint:CGPointMake(container.frame.origin.x,310)];

    CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    moveAnim.path = movePath.CGPath;
    moveAnim.removedOnCompletion = NO;
    moveAnim.fillMode = kCAFillModeForwards;


    CAAnimationGroup *animGroup = [CAAnimationGroup animation];
    animGroup.animations = [NSArray arrayWithObjects:moveAnim,nil];
    animGroup.duration = 2.0;
    animGroup.removedOnCompletion = NO;
    animGroup.fillMode = kCAFillModeForwards;

    [container.layer addAnimation:animGroup forKey:nil];
}
-(IBAction)moveUp:(id)sender{
     CGRect position = [[container.layer presentationLayer] frame];

    UIBezierPath *movePath = [UIBezierPath bezierPath];

    [movePath moveToPoint:CGPointMake(container.frame.origin.x,115)];

    CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    moveAnim.path = movePath.CGPath;
    moveAnim.removedOnCompletion = NO;
    moveAnim.fillMode = kCAFillModeForwards;

    CAAnimationGroup *animGroup = [CAAnimationGroup animation];
    animGroup.animations = [NSArray arrayWithObjects:moveAnim,nil];
    animGroup.duration = 2.0;
    animGroup.removedOnCompletion = NO;
    animGroup.fillMode = kCAFillModeForwards;

    [container.layer addAnimation:animGroup forKey:nil];

}

但行

CGRect position = [[container.layer presentationLayer] frame];

只返回目的位置而不是当前位置.一旦我释放按钮,我需要基本上给我一个动画的容器的当前位置,所以我可以执行下一个动画.我现在没有工作.

解决方法

我没有分析你的代码足够100%确定为什么[[container.layer presentationLayer] frame]可能不会返回你所期望的.但我看到几个问题.

一个明显的问题是moveDown:不声明movePath.如果movePath是一个实例变量,你可能想要清除它,或者在每次moveDown被调用时创建一个新的实例,但是我看不到你这样做.

一个不太明显的问题是(从使用removeOnCompletion和fillMode判断,尽管使用presentationLayer)您显然不了解Core Animation的工作原理.事实证明是令人惊讶的常见问题,所以原谅我,如果我错了.无论如何,请阅读,因为我会解释核心动画如何工作,然后如何解决您的问题.

在Core Animation中,您通常使用的图层对象是模型图层.当将动画附加到图层时,Core Animation将创建一个名为表示层的模型图层的副本,并且动画随着时间的推移更改表示层的属性.动画永远不会更改模型图层的属性.

当动画结束并且(默认情况下)被删除时,表示层被破坏,并且模型层的属性的值再次生效.因此,屏幕上的图层似乎会“回弹”到其原始位置/颜色/任何内容.

解决这个问题的常见但错误的方法是将动画的removeOnCompletion设置为NO,将其fillMode设置为kCAFillModeForwards.执行此操作时,表示层会挂起,所以屏幕上没有“快退”.问题在于,现在您将表现层挂在不同于模型层的值上.如果您要求模型层(或拥有它的视图)为动画属性的值,您将获得一个不同于屏幕上的值.如果您尝试再次动画该动画,动画可能会从错误的地方开始.

要动画化图层属性并使其“粘贴”,您需要更改模型图层的属性值,然后应用该动画.这样,当动画被移除并且表现层消失时,屏幕上的图层将看起来完全相同,因为模型层与动画结束时的表现层具有相同的属性值.

现在,我不知道为什么要使用关键帧来动画直线运动,或者为什么要使用动画组.在这里似乎都不需要.您的两种方法几乎相同,所以我们来解释一下常见的代码:

- (void)animateLayer:(CALayer *)layer toY:(CGFloat)y {
    CGPoint fromValue = [layer.presentationLayer position];
    CGPoint toValue = CGPointMake(fromValue.x,y);

    layer.position = toValue;

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.fromValue = [NSValue valueWithCGPoint:fromValue];
    animation.toValue = [NSValue valueWithCGPoint:toValue];
    animation.duration = 2;
    [layer addAnimation:animation forKey:animation.keyPath];
}

请注意,当我将动画添加到图层时,我们给动画一个关键.由于我们每次都使用相同的关键字,所以如果先前的动画尚未完成,每个新的动画将替换(删除)先前的动画.

当然,一旦你玩这个,你会发现,如果你moveUp:当moveDown:只有一半完成,moveUp:动画将看起来是一半的速度,因为它仍然有2秒的持续时间,但只有一半的旅行.我们应该根据要旅行的距离来计算持续时间:

- (void)animateLayer:(CALayer *)layer toY:(CGFloat)y withBaseY:(CGFloat)baseY {
    CGPoint fromValue = [layer.presentationLayer position];
    CGPoint toValue = CGPointMake(fromValue.x,y);

    layer.position = toValue;

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.fromValue = [NSValue valueWithCGPoint:fromValue];
    animation.toValue = [NSValue valueWithCGPoint:toValue];
    animation.duration = 2.0 * (toValue.y - fromValue.y) / (y - baseY);
    [layer addAnimation:animation forKey:animation.keyPath];
}

如果您真的需要它作为一个动画组中的关键字动画,您的问题应该告诉我们为什么你需要这些东西.无论如何,它也适用于这些东西:

- (void)animateLayer:(CALayer *)layer toY:(CGFloat)y withBaseY:(CGFloat)baseY {
    CGPoint fromValue = [layer.presentationLayer position];
    CGPoint toValue = CGPointMake(fromValue.x,y);

    layer.position = toValue;

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:fromValue];
    [path addLineToPoint:toValue];

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path = path.CGPath;
    animation.duration =  2.0 * (toValue.y - fromValue.y) / (y - baseY);

    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.duration = animation.duration;
    group.animations = @[animation];
    [layer addAnimation:group forKey:animation.keyPath];
}

您可以找到我的测试程序in this gist的完整代码.只需创建一个新的单视图应用程序项目,并将其内容替换为ViewController.m的内容.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...