1

iPad用のタブ付きアプリケーションがあります。最初のタブには、現在アクティブなメニューボタンを強調表示するポインタが付いた別のメニューがあります。ボタンをタップすると、ポインター(UIImage)が所定の位置にアニメーション化されます。

問題は、ハイライトされた画像を元の/デフォルトのボタン以外のボタンに残して、アプリケーションの別のタブに移動した後、再び元に戻すと、画像がデフォルトの場所に戻ってしまうことです。ただし、別のボタンをタップすると、画像が間違った方向に移動します。画像が戻っても古い座標が残っているからだと思いますか、それともその逆ですか?それは少しヒットとミスであり、iPadで直接テストする場合とシミュレータでテストする場合の動作が異なります。

私は絶対座標から始めて、プロセス全体を通してそれらに固執する必要があると思います。しかし、CGAffine関数を使用してそれを実現する方法について頭を悩ませることはできません。

これが私の現在のコードです

- (IBAction)diplayForm1:(id)sender {

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    buttonHighlightImage.transform = CGAffineTransformMakeTranslation(0, 0);
    [UIView commitAnimations];

}

- (IBAction)diplayForm2:(id)sender {

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    buttonHighlightImage.transform = CGAffineTransformMakeTranslation(0, 80);
    [UIView commitAnimations];

}

- (IBAction)diplayForm3:(id)sender {

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    buttonHighlightImage.transform = CGAffineTransformMakeTranslation(0, 160);
    [UIView commitAnimations];

}

編集 - - -

@Mundi私はあなたが提案したコードを試しました。newFrameとは、.h(「sfh」と呼びます)で行った新しいUIImageViewとプロパティを定義し、次に.mで@synthesizeを定義することを意味すると想定しています。

これは.mの私のコードです

-(void)viewWillAppear:(BOOL)animated {

    // The "pointer" and the "old" frame are the same so I thought it pointless to do
    // selectedFormHighlight.frame = selectedFormHighlight.frame;

    // I tried anyway and also tried this and many other combinations
    selectedFormHighlight.frame = sfh.frame;

}

-(void)viewDidAppear:(BOOL)animated {

    [UIView beginAnimations:nil context:NULL];
    [UIView animateWithDuration:0.3 animations:^{
        selectedFormHighlight.frame = CGRectMake(0,0,0,0);
    }];
    [UIView commitAnimations];

}

これを実行すると、ポインタが画面(左上)からアニメーション化され、戻りません。viewWillAppearとviewDidAppearで考えられるすべての可能な組み合わせを試しましたが、違いはありません。viewDidAppearの他の行を削​​除しようとしましたが、それでも同じことを行います。

ユーザーが別の画面から戻ったときに、ポインターを元の(デフォルトの)位置からアニメーション化することを提案していると思います。私はそれを望んでいません、それは彼らがアニメーションなしでそれを残したのと同じ場所に単にとどまるべきです。または、少なくとも彼らが気付かないうちにそこに戻します。唯一のアニメーションは、そのビュー内の別のボタンをタップすることを選択した場合です。

4

2 に答える 2

0

@Mundi の助けに感謝しますが、そのように機能させることはできませんでした。最終的に次のコードにたどり着きました。

// This initially sets the frame with a variable value that 
// corresponds to the current child view
-(void)viewDidAppear:(BOOL)animated {
    selectedFormHighlight.frame = CGRectMake(0, self.getY, 250, 4100);
}

// Button actions (converted to a single action instead of one for each button)
// This was done by referencing a tag added to each button (the button tags 
// match the children's tags)
-(IBAction)goToForm:(id)sender {
    UIButton *btn = (UIButton *)sender;
    self.currentChild = self.formViewControllers[btn.tag];
    [UIView beginAnimations.nil context:NULL];
    [UIView animateWithDuration:0.3 animations:^{
        selectedFormHighlight.frame = CGRectMake(0, self.getY, 250, 4100);
    }];
    [UIView commitAnimations];
    sfh.frame = selectedFormHighlight.frame;
}

// This returns the 'y' (vertical center position) of the pointer 
// frame for each of the child views
-(int)getY {
    switch(_currentChild.view.tag) {
        case 1:
            return -2005;
            break;
        case 2:
            return -1925;
            break;
        default:
            return -1845;
            break;
    }
}
于 2013-02-22T16:16:36.247 に答える
0

これが発生している理由は、ビューを非表示にして再表示すると、実際のビュー オブジェクトとそのプレゼンテーション レイヤーが混同されるためです。解決策は、レイヤーをアニメーション化するのではなく、ビュー プロパティを変更することです。

したがって、アフィン変換を使用する必要はありません。frameまたはを変更して、ポインター ビューの新しい位置を直接設定するだけcenterです。

これを行う最新の形式は、ブロック ベースのアニメーションです。

[UIView animateWithDuration:0.5 animations:^{
   pointer.frame = newFrame;
}];

また、変数が 1 つ異なるだけで 3 つのメソッドを使用するのは非常に非効率的です。目的の y 位置に 80 を掛けるだけです。

編集

ボタンがリセットされるもう 1 つの理由は、ビューがインスタンス化されたときにボタンが置かれた場所だからです。ビュー コントローラでビューの位置を設定し、.viewWillAppearで新しい位置にアニメーション化するだけviewDidAppearです。

于 2013-02-20T08:25:23.620 に答える