1

アニメーションで右の検索バーを左に20px長くし、左の検索バーを右に20px短くしたいです。どうやってやるの?

CGRect leftFrame = self.leftSearchBar.frame;
    //leftFrame.origin.x = leftFrame.origin.x - 20;
    leftFrame.size.width = leftFrame.size.width - 40;

    CGRect rightFrame = self.rightSearchBar.frame;
    rightFrame.origin.x = rightFrame.origin.x - 40;
    rightFrame.size.width = rightFrame.size.width + 40;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelay:0.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

    self.leftSearchBar.frame = leftFrame;
    self.rightSearchBar.frame = rightFrame;

    [UIView commitAnimations];

うまくいきません。最初にアニメーションなしでビューのサイズを変更し、次にアニメーション付きで右側のビューを移動します。

4

1 に答える 1

1

アンカーポイントなどを変更していないと仮定すると、幅を拡大するだけで右に伸びます。これをUIViewアニメーションブロック内にラップすると、質問内の仕様に従って次のようにアニメーション化する必要があります。

UISearchBar* searchbar = ...; // However you've created your UISearchBar, we'll refer to it as 'searchbar'

CGRect searchFrame = searchbar.frame;
[UIView animateWithDuration:0.8 
                      delay:0.0
                    options:UIViewAnimationOptionBeginFromCurrentState
                 animations:^{
                     searchFrame.size.width += 30;
                     searchbar.frame = searchFrame;
                 }
                 completion:nil];

+= 30が複数回呼び出されないようにする必要があります。そうしないと、呼び出されるたびに30ポイントずつ拡張されます。事前にサイズがわかっている場合は、単にそれを= size + 30に置き換えることができます。これは、アニメーションを複数回呼び出す場合に安全な方法です。

于 2012-11-27T22:15:04.207 に答える