0

アニメーションを形成するために反復する画像の配列を含むUIImageViewがあります。

[imageView setAnimationImages:arrayOfImages];

次に、アニメーション中にこの画像ビューをパスに沿って移動する必要があります。この例のソースコードを調べました。UIBezierPathを使用してパスを作成し、CALayerを使用してパスに沿って移動するオブジェクトを表します。しかし、UIImageViewをアニメーション化するためにこれをどのように行うのでしょうか?

アドバイスをありがとうございます。

4

1 に答える 1

1

[編集]UIImageViewをサブビューとして現在のビューに追加する必要があることに注意してください。

最後に、前に示した例を使用してそれを理解しました。これが私のステップです:

  1. パスを作成します。(以下のコードのP = CGPointMakeに注意してください)

    UIBezierPath *trackPath = [UIBezierPath bezierPath];
    [trackPath moveToPoint:P(160, 25)];
    [trackPath addCurveToPoint:P(300, 120)
             controlPoint1:P(320, 0)
             controlPoint2:P(300, 80)];
    [trackPath addCurveToPoint:P(80, 380)
             controlPoint1:P(300, 200)
             controlPoint2:P(200, 480)];
    

    ...。

  2. UIImageViewを作成し、アニメーション画像の配列を指定します。

    UIImageView *test = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [test setAnimationImages:[NSArray arrayWithObjects:[UIImage imageNamed:@"bee-1"],    
    [UIImage imageNamed:@"bee-2"], nil]];
    [test startAnimating];
    
  3. UIImageViewのレイヤー位置を設定し、ビューのレイヤーにレイヤーを追加します。

    [test.layer setPosition:P(160,25)];
    [self.view.layer addSublayer:test.layer];
    
  4. CAKeyframeAnimationを作成します。

    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    anim.path = trackPath.CGPath;
    anim.rotationMode = kCAAnimationRotateAuto;
    anim.repeatCount = HUGE_VALF;
    anim.duration = 8.0;
    [test.layer addAnimation:anim forKey:@"race"];
    
于 2012-10-09T03:19:18.317 に答える