1

画像(UIView)の回転と移動に問題があります。

これを回転に使用する場合:

[UIView beginAnimations: @"rotate" contex: nil];
[UIView setAnimationDuration: 1];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
spaceShip.transform = CGAffineTransformMakeRotation (angle); // spaceShip is the UIView
[UIView commitAnimations];

そしてこれを動かす:

[UIView beginAnimations: @"move" contex: nil];
[UIView setAnimationDuration: 1];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
spaceShip.center = destanation // destanation is a point where user tap
[UIView commitAnimations];

ひとつひとつうまくいっています。しかし、この 2 つのアニメーションを一緒に使用することはできません。宇宙船は回転していますが、方向は変わりません。

私の間違いはどこですか?

PSアーリーアンカーポイント(0.5、0.5)を指定しました。しかし、画像を回転させると座標が変わります。

更新:

ブロックベースの方法を使用。このような

- (void) viewDidLoad
{
    [spaceShip.layer setAnchorPoint: CGPointMake(0.5, 0.5)];

}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *tap = [[event allTouches] anyObject];
    destanationPoint = [tap locationInView: tap.view];

    NSLog(@"%0.3f", angle*180/M_PI);//Just for monitoring

    [UIView animateWithDuration:1.0
                          delay: 0.0
                        options: UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         spaceShip.transform = CGAffineTransformMakeRotation(angle);
                     }
                     completion:^(BOOL finished){
                         // Wait one second and then fade in the view
                         [UIView animateWithDuration:1.0
                                               delay: 1.0
                                             options: UIViewAnimationOptionCurveEaseOut
                                          animations:^{
                                              spaceShip.center = destanationPoint;
                                          }
                                          completion:nil];
                     }];

}

その方法は有効です。ただし、新しいタップごとにUIImageが開始位置の座標を変更します。わかりました、追加してみました:

options: UIViewAnimationOptionCurveEaseOut |
         UIViewAnimationOptionBeginFromCurrentState

しかし、それは助けにはなりません。

UPD2

これを試してください:

[UIView animateWithDuration:1.0

                          delay: 0.0
                        options: UIViewAnimationOptionCurveEaseOut |
                                UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         spaceShip.transform = CGAffineTransformMakeRotation(angle);
                         spaceShip.center = destanationPoint;
                     }
                     completion:^(BOOL finished){
                         // Wait one second and then fade in the view
                         [UIView animateWithDuration:1.0
                                               delay: 1.0
                                             options: UIViewAnimationOptionCurveEaseOut |
                                                    UIViewAnimationOptionBeginFromCurrentState
                                          animations:^{
                                              spaceShip.center = destanationPoint;
                                          }
                                          completion:nil];
                     }];

今、私の船は回転し、タップポイントに移動します. 別のポイントでの2回目のタップで UIView は開始座標に戻りますが....アニメーション付き ))

4

5 に答える 5

1

スタック オーバーフローへようこそ :)

UIView Class Referenceを読んだ場合:

いくつかのビュー プロパティへの変更はアニメーション化できます。つまり、プロパティを変更すると、短い時間でユーザーに変更を伝えるアニメーションが作成されます。UIView クラスは、実際のアニメーションを実行する作業のほとんどを行いますが、アニメーション化するプロパティの変更を指定する必要があります。アニメーションを開始するには、次の 2 つの方法があります。

  • iOS 4 以降では、ブロックベースのアニメーション メソッドを使用します。(おすすめされた)
  • begin/commit アニメーション メソッドを使用します。

ブロックベースのアニメーション メソッド (animateWithDuration:animations: など) により、アニメーションの作成が大幅に簡素化されます。1 回のメソッド呼び出しで、実行するアニメーションとアニメーションのオプションを指定します。ただし、ブロックベースのアニメーションは iOS 4 以降でのみ使用できます。アプリケーションが以前のバージョンの iOS で実行される場合は、beginAnimations:context: および commitAnimations クラス メソッドを使用して、アニメーションの開始と終了をマークする必要があります。

したがって、ブロック メソッドに切り替えることをお勧めします。

このテーマをカバーするいくつかの優れたリンクを次に示します。

さて、特にあなたの問題のために、回転と動きを同じアニメーション ブロックに入れてみましたか? [UIView setAnimationBeginsFromCurrentState: YES];特にwhich を使用すると、現在発生しているアニメーションが中断されると思われるため、この 2 つが相殺されている可能性があります。

于 2012-11-01T05:02:19.187 に答える
0

UIViewでアニメーションを設定するための簡単なコードです

// transform make rotation for the view
     view.transform = CGAffineTransformMakeRotation(M_PI_2*1);
         [UIView commitAnimations];  

    // it used to move view with animation
    [UIView animateWithDuration:2
                              delay:1.0
                            options: UIViewAnimationCurveEaseOut
                         animations:^{
                            //set your new frame for UIView
                         } 
                         completion:^(BOOL finished){
                             NSLog(@"Done!");
                         }];
于 2012-11-01T05:16:42.003 に答える
0

交換するだけ

spaceShip.center = destination 

spaceShip.center = CGPointMake(destination.x, destination.y);

UPD

NSLOGを追加します(CGPoint宛先の後)

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *tap = [[event allTouches] anyObject];
    CGPoint destanation = [tap locationInView: tap.view];
    NSLog(@" x=%f and y=%f", destination.x, destination.y
          );
于 2012-11-01T10:23:05.410 に答える
0

このようにしてみて、

{
    [UIView beginAnimations: @"rotate" contex: nil];
    [UIView setAnimationDuration: 1];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationCurve: UIViewAnimationCurveLinear];
    spaceShip.transform = CGAffineTransformMakeRotation (angle); // spaceShip is the UIView
    [UIView setAnimationDidStopSelector: @selector(animationStopped)];
    [UIView commitAnimations];
}

それから、

-(void)animationStopped {
[UIView beginAnimations: @"move" contex: nil];
[UIView setAnimationDuration: 1];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
spaceShip.center = destanation // destanation is a point where user tap
[UIView commitAnimations];
}
于 2012-11-01T04:59:07.550 に答える