2012-09-06 3 views
11

CGPointをあるビューから別のビューに移動するときにアニメーションを実行しようとしていますが、ポイントを参照する座標を見つける必要があります。最初にアニメーションを行うことができます。アニメーションのあるビューから別のビューへCGPointsを相対的に変換する

ビュー2にポイント(24,15)があり、それをビュー1にアニメーションしたいとします。ポイントを追加する際に、ポイントの値を新しいビュー内に保持したいとします新しいビューのサブビューとして、私はトゥイーンを行うことができるので、ポイントがどこになるのかを知る必要があります。

このグラフを参照してください:

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]; 
}]; 

dot1をview1に追加すると、view2から削除されます。オフセットベクトルがドットをその境界の外に移動する場合、view1にはclipsToBounds == NOがあるはずです。

関連する問題