我使用层属性使用 CAAnimation 为我的 UIImageView 设置动画,如下所示:
[imageView.layer addAnimation:pathAnimation forKey:[NSString stringWithFormat:@"pathAnimation%@",objId]];
但是在我的动画结束时(在对象从原始点移动之后)当我读取帧的 X Y 坐标或中心坐标时,它们总是显示为原始坐标,而不是对象移动到的坐标。
如何读取层的坐标以便确定移动对象的正确位置?
这是我的代码:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
imageView.image = [UIImage imageNamed:@"banner1.jpg"];
imageView.animationDuration = 2;
imageView.animationRepeatCount = 0;
imageView.tag = 100000;
imageView.layer.frame = imageView.frame;
[self.view addSubview:imageView];
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.duration = 2.0f;
pathAnimation.delegate=self;
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
CGMutablePathRef pointPath = CGPathCreateMutable();
CGPathMoveToPoint(pointPath, NULL, 100, 100);
CGPathAddLineToPoint(pointPath, NULL, 300, 200);
CGPathAddLineToPoint(pointPath, NULL, 200, 150);
pathAnimation.path = pointPath;
CGPathRelease(pointPath);
[imageView.layer addAnimation:pathAnimation forKey:@"pathAnimation"];
[imageView release];
最佳答案
如果您为图层设置动画,包含图层的 View 将保留在同一位置,因此帧将保持不变。层也有 frame、bound 和 position 属性,因此请尝试阅读层的属性。
编辑:
我在 CAKeyframeAnimation Class Reference 中找到了以下解释。
While animating, it updates the value of the property in the render tree with values calculated using the specified interpolation calculation mode.
为了了解什么是渲染树,我在这里找到了它。
似乎渲染树中的值正在更新,并且由于渲染树不同于演示文稿并且是私有(private)的,我们无法访问它。
以下是上述链接的最后一段。
You can query an instance of CALayer for its corresponding presentation layer while an animation transaction is in process. This is most useful if you intend to change the current animation and want to begin the new animation from the currently displayed state.
基于此,解决方法可以是根据您提供的路径计算层的新位置,并相应地设置表示层框架属性。
如果您发现任何更新,请写在这里......
关于iphone - 对象移动后的 CALayer 定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7336529/