12

我正在尝试制作一个动画,将一个 CGPoint 从一个视图移动到另一个视图,我想找到该点相对于第一个视图的坐标,以便我可以制作动画。

因此,假设我在 view2 中有一个点 (24,15),并且我想将其动画化到 view1,我仍然想在新视图中保留该点的值,因为我将该点添加为新视图,但对于动画,我需要知道该点所在位置的值,以便我可以进行补间。

请参考这张图:

在此处输入图像描述

现在这就是我想要做的:

customObject *lastAction = [undoStack pop];
customDotView *aDot = lastAction.dot;
CGPoint oldPoint = aDot.center;
CGPoint  newPoint = lastAction.point;

newPoint = [lastAction.view convertPoint:newPoint toView:aDot.superview];


CABasicAnimation *anim4 = [CABasicAnimation animationWithKeyPath:@"position"];
anim4.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
anim4.fromValue = [NSValue valueWithCGPoint:CGPointMake(oldPoint.x, oldPoint.y )];
anim4.toValue = [NSValue valueWithCGPoint:CGPointMake( newPoint.x,  newPoint.y )];
anim4.repeatCount = 0;
anim4.duration = 0.1;
[aDot.layer addAnimation:anim4 forKey:@"position"];


[aDot removeFromSuperview];


[lastAction.view addSubview:aDot];
[lastAction.view bringSubviewToFront:aDot];

aDot.center = newPoint;

有任何想法吗?

4

1 回答 1

9

使用块动画更容易看到。我认为目标是在其坐标空间中制作 view2 子视图的动画,然后,当动画完成时,使用转换为新坐标空间的结束位置将子视图添加到 view1。

// assume we have a subview of view2 called UIView *dot;
// assume we want to move it by some vector relative to it's initial position
// call that CGPoint offset;

// compute the end point in view2 coords, that's where we'll do the animation
CGPoint endPointV2 = CGPointMake(dot.center.x + offset.x, dot.center.y + offset.y);

// compute the end point in view1 coords, that's where we'll want to add it in view1
CGPoint endPointV1 = [view2 convertPoint:endPointV2 toView:view1];

[UIView animateWithDuration:1.0 animations:^{
    dot.center = endPointV2;
} completion:^(BOOL finished) {
    dot.center = endPointV1;
    [view1 addSubview:dot];
}];

请注意,将点添加到 view1 会将其从 view2 中删除。另请注意,如果clipsToBounds == NO偏移向量将点移动到其边界之外,则 view1 是否应该具有。

于 2012-09-08T23:37:50.833 回答