2009-05-14 11 views

答えて

1

最も簡単な方法は、用途UIView animations

あなたがあなたのポイントを保持するために、あなたのイメージとNSArrayのを保持するためにUIImageViewを使用することができますを前提と簡単な例にあります。私のお勧めのアプローチはUIImageViewにUIImageをラップし、あなたの三点を通る経路に沿って、あなたのUIImageViewの層をアニメーション化するCAKeyframeAnimationを使用することです

UIImage *image = [UIImage imageNamed:@"image.png"]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

    [someView addSubview:imageView]; // Assume someView exists 

    NSValue *firstPoint = [NSValue valueWithCGPoint:CGPointMake(0, 0)]; 
    NSValue *secondPoint = [NSValue valueWithCGPoint:CGPointMake(100, 0)]; 
    NSValue *thirdPoint = [NSValue valueWithCGPoint:CGPointMake(100, 100)]; 
    // And so on.... 

    NSArray *points = [NSArray arrayWithObjects:firstPoint, secondPoint, thirdPoint, nil]; 

    for (NSValue *pointValue in points) { 

     [UIView beginAnimations:@"UIImage Move" context:NULL]; 

     CGPoint point = [pointValue CGPointValue]; 
     CGSize size = imageView.frame.size; 

     imageView.frame = CGRectMake(point.x, point.y, size.width, size.height); 

     [UIView commitAnimations]; 
    } 
+3

実際には、3つの動作を同時に開始するため、正しく動作しません。アニメーションはバックグラウンドスレッドで実行され、完了するのを待つ必要があります。パスに沿って移動するのではなく、アニメーションの動作は予測できません。 –

+0

何も言えません、あなたは正しいです! – klaaspieter

9

:デフォルトであること

UIImage *image = [UIImage imageNamed:@"image.png"]; 
imageView = [[UIImageView alloc] initWithImage:image]; 
[mainView addSubview:imageView]; 
// Remember to remove the image view and release it when done with it 

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
pathAnimation.duration = 1.0f; 
pathAnimation.calculationMode = kCAAnimationPaced; 
pathAnimation.fillMode = kCAFillModeForwards; 
pathAnimation.removedOnCompletion = NO; 

CGMutablePathRef pointPath = CGPathCreateMutable(); 
CGPathMoveToPoint(pointPath, NULL, viewOrigin.x, viewOrigin.y); 
CGPathAddLineToPoint(pointPath, NULL, point1.x, point1.y); 
CGPathAddLineToPoint(pointPath, NULL, point2.x, point2.y); 
CGPathAddLineToPoint(pointPath, NULL, point3.x, point3.y); 
pathAnimation.path = pointPath; 
CGPathRelease(pointPath); 

[imageView.layer addAnimation:pathAnimation forKey:@"pathAnimation"]; 

注意、レイヤの位置はレイヤの中央にあります。レイヤーを別の参照ポイントに相対的に移動したい場合は、レイヤーのanchorPointプロパティを左上隅の(iPhoneの場合)(0.0、0.0)、下層の場合の(0.0、1.0)左。

また、これでUIImageViewのフレームが変更されることはありません。後でそのフレームを参照する場合は、そのことを考慮に入れるか、デリゲートメソッドのコールバックを追加する必要がありますアニメーションを適切な値に設定します。

CGPathAddLineToPoint()の呼び出しをCGPathAddCurveToPoint()に置き換えることで、直線ではなく曲線に沿って画像を移動することもできます。

EDIT(5/14/2009):欠落しているpathAnimation.path = pointPath行を追加し、curvePathの誤った参照をpointPathに変更しました。

+0

これは正解です –

+0

私はこのアニメーションも実行されていないように遅れを引き起こしている(マルチスレッド)の処理をたくさん持っている、それはちょうど非常にジャンプ... CAKeyFrameAnimationをスレッドに配置する方法はありますか実行することを最優先にしますか? –

関連する問題