Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在动画中找到CAlayer的位置?_Iphone_Ios - Fatal编程技术网

Iphone 如何在动画中找到CAlayer的位置?

Iphone 如何在动画中找到CAlayer的位置?,iphone,ios,Iphone,Ios,我正在实现一个游戏应用程序。在这个应用程序中,我使用层来制作动画 CGMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path, NULL, previousValuex, previousValue); CGPathAddLineToPoint(path, NULL, valuex, value); previousValue=value; previousValuex=valuex; CAKeyframeAnim

我正在实现一个游戏应用程序。在这个应用程序中,我使用层来制作动画

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, previousValuex, previousValue);
CGPathAddLineToPoint(path, NULL, valuex, value);
previousValue=value;
previousValuex=valuex;

CAKeyframeAnimation *animation;
animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.path = path;
animation.duration =1.0;
animation.repeatCount = 0;
//animation.rotationMode = kCAAnimationRotateAutoReverse;
animation.calculationMode = kCAAnimationPaced;

// Create a new layer for the animation to run in.
CALayer *moveLayer = [imgObject layer];
[moveLayer addAnimation:animation forKey:@"position"];

现在我想在动画过程中找到层位置?可能吗?请帮助我。

我从未尝试过这样做,但您应该能够通过KVO找到层位置?监视CALayer的帧属性或位置、边界或主播点,具体取决于动画过程中所需的内容。

我从未尝试过这样做,但您应该能够通过KVO?监视CALayer的帧属性或位置、边界或锚点,具体取决于动画期间所需的内容。

要在动画期间找到当前位置,需要查看层的presentationLayer的属性。层本身的特性将仅反映隐式动画的最终目标值,或应用CABASICANIATION之前的初始值。presentationLayer为您设置动画的任何属性提供瞬时值

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, previousValuex, previousValue);
CGPathAddLineToPoint(path, NULL, valuex, value);
previousValue=value;
previousValuex=valuex;

CAKeyframeAnimation *animation;
animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.path = path;
animation.duration =1.0;
animation.repeatCount = 0;
//animation.rotationMode = kCAAnimationRotateAutoReverse;
animation.calculationMode = kCAAnimationPaced;

// Create a new layer for the animation to run in.
CALayer *moveLayer = [imgObject layer];
[moveLayer addAnimation:animation forKey:@"position"];
比如说,

CGPoint currentPosition = [[moveLayer presentationLayer] position];

当图层围绕您的路径设置动画时,将获取图层的当前位置。不幸的是,我认为很难在演示层中使用关键值观察,因此如果要跟踪它,您可能需要手动轮询该值。

为了在动画期间找到当前位置,您需要查看该层的presentationLayer的属性。层本身的特性将仅反映隐式动画的最终目标值,或应用CABASICANIATION之前的初始值。presentationLayer为您设置动画的任何属性提供瞬时值

比如说,

CGPoint currentPosition = [[moveLayer presentationLayer] position];

当图层围绕您的路径设置动画时,将获取图层的当前位置。不幸的是,我认为很难在表示层中使用键值观察,因此如果要跟踪它,您可能需要手动轮询此值。

如果您的CALayer位于另一CALayer内,则可能需要应用父CALayer的仿射变换来获得子CALayer的位置,如下所示:

// Create your layers
CALayer *child = CALayer.layer;
CALayer *parent = self.view.layer;
[parent addSubLayer:child];

// Apply animations, transforms etc...

// Child center relative to parent
CGPoint childPosition = ((CALayer *)child.presentationLayer).position;

// Parent center relative to UIView
CGPoint parentPosition = ((CALayer *)parent.presentationLayer).position;
CGPoint parentCenter = CGPointMake(parent.bounds.size.width/2.0, parent.bounds.size.height /2.0);

// Child center relative to parent center
CGPoint relativePos = CGPointMake(childPosition.x - parentCenter.x, childPosition.y - parentCenter.y);

// Transformed child position based on parent's transform (rotations, scale etc)
CGPoint transformedChildPos = CGPointApplyAffineTransform(relativePos, ((CALayer *)parent.presentationLayer).affineTransform);

// And finally...
CGPoint positionInView = CGPointMake(parentPosition.x +transformedChildPos.x, parentPosition.y + transformedChildPos.y);

这段代码基于我刚刚编写的代码,其中父CALayer正在旋转,位置正在更改,我想在父CALayer所属的UIView中获取子CALayer相对于触摸位置的位置。这是基本的想法,但我还没有实际运行这个伪代码版本。

如果您的CALayer在另一个CALayer中,您可能需要应用父CALayer的仿射变换来获得子CALayer的位置,如下所示:

// Create your layers
CALayer *child = CALayer.layer;
CALayer *parent = self.view.layer;
[parent addSubLayer:child];

// Apply animations, transforms etc...

// Child center relative to parent
CGPoint childPosition = ((CALayer *)child.presentationLayer).position;

// Parent center relative to UIView
CGPoint parentPosition = ((CALayer *)parent.presentationLayer).position;
CGPoint parentCenter = CGPointMake(parent.bounds.size.width/2.0, parent.bounds.size.height /2.0);

// Child center relative to parent center
CGPoint relativePos = CGPointMake(childPosition.x - parentCenter.x, childPosition.y - parentCenter.y);

// Transformed child position based on parent's transform (rotations, scale etc)
CGPoint transformedChildPos = CGPointApplyAffineTransform(relativePos, ((CALayer *)parent.presentationLayer).affineTransform);

// And finally...
CGPoint positionInView = CGPointMake(parentPosition.x +transformedChildPos.x, parentPosition.y + transformedChildPos.y);

这段代码基于我刚刚编写的代码,其中父CALayer正在旋转,位置正在更改,我想在父CALayer所属的UIView中获取子CALayer相对于触摸位置的位置。这是基本的想法,但我还没有实际运行这个伪代码版本。

实际上,在这种情况下,这不起作用。虽然可以观察图层的特性,但它们只反映图层的开始值或结束值,而不反映两者之间的任何值。正如我在回答中所阐明的,在制作动画时,您需要查看presentationLayer的当前值。啊,正如我所指出的,我从未尝试过我所建议的!。无论如何,我已经把你的答案投了赞成票。如果我将来需要做类似的事情,我很高兴知道。事实上,这在这种情况下是行不通的。虽然可以观察图层的特性,但它们只反映图层的开始值或结束值,而不反映两者之间的任何值。正如我在回答中所阐明的,在制作动画时,您需要查看presentationLayer的当前值。啊,正如我所指出的,我从未尝试过我所建议的!。无论如何,我已经把你的答案投了赞成票。如果我将来需要做类似的事情,很高兴知道。你可能知道如何获得presentationLayer的规模吗?[[presentationLayer valueForKeyPath:@transform.scale]floatValue]返回最终值而不是当前值。您可能知道如何获得presentationLayer的比例吗?[[presentationLayer valueForKeyPath:@transform.scale]floatValue]返回最终值,而不是当前值。