Objective c 将动画的CGP点从一个视图转换为另一个视图

Objective c 将动画的CGP点从一个视图转换为另一个视图,objective-c,ios,c,Objective C,Ios,C,我试着做一个动画,我把一个CG点从一个视图移到另一个视图,我想找到点相对于第一个视图的坐标,这样我就可以做动画了 假设我在view2中有一个点(24,15),我想将其设置为view1的动画,我仍然希望在新视图中保留该点的值,因为我将该点添加为新视图的子视图,但是对于动画,我需要知道该点的位置值,以便我可以进行二者之间的转换 请参考此图: 这就是我要做的: customObject *lastAction = [undoStack pop]; customDotView *aDot = last

我试着做一个动画,我把一个CG点从一个视图移到另一个视图,我想找到点相对于第一个视图的坐标,这样我就可以做动画了

假设我在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;

有什么想法吗?

使用块动画更容易看到。我认为目标是在其坐标空间中制作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中删除。另请注意,如果偏移向量将点移动到其边界之外,则如果view1应具有
clipsToBounds==NO

这两个视图在显示中都可见吗?如中所示,它们是否包含在更大的视图中?