1

UIView を左に移動するコードを以下に示します。

- (void)viewDidLoad {
    [super viewDidLoad];

    [UIView beginAnimations:nil context:NULL];

        [UIView setAnimationDuration:4];

        int xOriginal = 91;

        CGRect rect = imagem.frame;

        int x = rect.origin.x;
        int y = rect.origin.y;
        int w = rect.size.width;
        int h = rect.size.height;

        if(x == xOriginal){

            imagem.frame = CGRectMake(x+100, y, w, h);
        }else{

            imagem.frame = CGRectMake(x-100, y, w, h);
        }

        [UIView commitAnimations];

}

私のビューの座標は x = 91 (スーパービューの中心) です。アプリを起動すると、UIView は中央から右に移動するのではなく、左に移動して中央に移動します。これはなぜですか?

左から中央ではなく、UIView を中央 (x=91) から右 (91+100) に移動するにはどうすればよいですか?

4

4 に答える 4

2
[UIImageView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveLinear  animations:^{
                imagem.frame=CGRectMake(imagem.frame.origin.x+100, imagem.frame.origin.y, imagem.frame.size.width, imagem.frame.size.height);

   } completion:^(BOOL finished) {
                //code for completion
                NSLog(@"Animation complete");
            }];
于 2014-12-19T11:06:00.797 に答える
1
Initially image.frame = CGRectMake(91,100,20,20);
So imageview starting point is 0

[UIView animateWithDuration:5.0
  animations:^{
    //Animation code goes here
   image.frame = CGRectMake(191,100,20,20); //Now imageview moves from 0 to 100
  } completion:^(BOOL finished) {
    //Code to run once the animation is completed goes here
}];
于 2014-12-19T10:59:10.247 に答える
1

自動レイアウトとフレーム アニメーションは相性が悪いです。フレームを直接アニメーション化するのではなく、拘束をアニメーション化してみてください。

制約のアウトレットを作成constantし、コードで制約のプロパティを設定してビューを移動できます。また、アニメーション ブロック内で制約を更新するために
呼び出します。-[view layoutIfNeeded]

それ以外の場合は、ビューの制約をすべて削除して、そのフレームを大胆にアニメーション化できます。

于 2014-12-19T11:09:47.470 に答える