2012-09-06 102 views
11

我试图做一个动画,我将一个CGPoint从一个视图移动到另一个视图,我想找到点的坐标将参考首先,我可以做动画。将CGPoint从一个视图转换为另一个视图相对于动画

所以我们假设我在view2中有一个点(24,15),并且我想将它设为view1的动画,我仍然想要在新视图中保留点的值,因为我添加了点作为新视图的子视图,但对于动画我需要知道点的位置的价值,所以我可以做一个补间。

请参考此图:

enter image description here

现在,这是我想要做的事:

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; 

任何想法?

+0

这两个视图都可以显示吗?它们是否包含在更大的视图中? –

回答

8

用块动画更容易看到。我认为目标是在坐标空间中执行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如果偏移向量移动它的边界外的点。

相关问题