0

uiimageを右(画面外)から中央にスライドさせてから、中央から左(画面外)にスライドさせたい。画像が途中(アニメーションパスの最初のポイント)に到着したら、カスタム関数を呼び出します。どうすればそれを実現できますか?

手伝ってくれてありがとう。

それは私が試したものですが、途中(2番目のアニメーションの開始)で少し遅れています。アニメーションが1つある方がいいと思います

私のコード:

- (void) animateFromRightToMiddlePath {

 CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
 pathAnimation.calculationMode = kCAAnimationPaced;
 pathAnimation.fillMode = kCAFillModeForwards;
 pathAnimation.removedOnCompletion = NO;
 pathAnimation.duration = 3;
 pathAnimation.repeatCount = 1;
 pathAnimation.delegate = self;
 [pathAnimation setValue:@"rightToMiddle" forKey:@"AnimationType"];


 CGMutablePathRef curvedPath = CGPathCreateMutable();
 CGPathMoveToPoint(curvedPath, NULL, x + (picture.size.width/2), 250);
 CGPathAddLineToPoint(curvedPath, NULL, (picture.size.width/2), 250);
 pathAnimation.path = curvedPath;
 CGPathRelease(curvedPath);
 imageView = [[UIImageView alloc] initWithImage:picture];
 imageView.tag = 1;
 imageView.frame = CGRectMake(1, 1, picture.size.width, picture.size.height);
 [self addSubview:imageView];
 [imageView.layer addAnimation:pathAnimation forKey:@"moveTheSquare"];

}

- (void) animateFromMiddleToLeftPath {

     CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
     pathAnimation.calculationMode = kCAAnimationPaced;
     pathAnimation.fillMode = kCAFillModeForwards;
     pathAnimation.removedOnCompletion = NO;
     pathAnimation.duration = 3; 
     pathAnimation.repeatCount = 1;
     pathAnimation.delegate = self;
     [pathAnimation setValue:@"middleToLeft" forKey:@"AnimationType"];

     CGMutablePathRef curvedPath = CGPathCreateMutable();
     CGPathMoveToPoint(curvedPath, NULL, (picture.size.width/2), 250);
     CGPathAddLineToPoint(curvedPath, NULL, -(picture.size.width/2), 250);
     pathAnimation.path = curvedPath;
     CGPathRelease(curvedPath);

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

}

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{

    NSString *aniType = [theAnimation valueForKey:@"AnimationType"];

    if ([aniType isEqualToString:@"rightToMiddle"]) {

        [self animateFromMiddleToLeftPath];
     // CUSTOM FUNCTION CALL
    }

    if ([aniType isEqualToString:@"middleToLeft"]) {

//        [self animateFromRightToMiddlePath];

    }

}
4

1 に答える 1

0

この種の相互作用をモデル化する方法は次のとおりです。

  1. 画像は独自のロジック(固定アニメーション、ドラッグなど)に従って画面内を移動します。

  2. 各ティックでハンドラー関数が呼び出されるようにタイマーを定義します。このハンドラーには、(特定の場合に)画像の位置をチェックする責任があります。

  3. 位置が希望どおりになると、ハンドラーは他の関数を起動します。

これは複雑に見えるかもしれませんが、実際にはそうではありません。モジュール式で再利用可能な方法でこの問題を解決することができ、ゲームがこの種の問題をどのように処理するかに触発されています。

于 2011-07-19T08:35:31.310 に答える