5

私は2つのビューを切り替えようとしています。それは簡単です。コードは以下にありますが、フリップを実行するために使用されるボタンも同時にフリップしたいと考えています。

この動作は、トラックを再生しているときに iPod アプリケーションで確認できます。フリップ ボタンをタップすると、カバー アートとトラック リストが交互に表示されますが、同時にボタンが反転します。

これはナビゲーション コントローラーのページで、フリップしたいボタンは ですrightBarButtonItem

ここに私がこれまで持っているコードがあります。これはビューを反転しますが、rightBarButton は反転しません。

[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: 0.5f];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
showingBackside = !showingBackside;
if (showingBackside) {
    [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft
                           forView: self.view
                             cache: YES];
    [self.view addSubview: backside.view];
    [frontside.view removeFromSuperview];
} else {
    [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight
                           forView: self.view
                             cache: YES];
    [self.view addSubview: frontside.view];
    [backside.view removeFromSuperview];
}
// flip image, too
NSString *newImage = showingBackside ? @"backside.png" : @"frontside.png";
[(self.navigationItem.rightBarButtonItem) setImage: [UIImage imageNamed: newImage]];
[UIView commitAnimations];

(ここにある画像フリッピング コードはコンパイルできない可能性があります。何をしようとしているのかを説明するために後で追加しました。)

問題が発生しているのは、ナビゲーションコントローラーの右端のボタンを変更して、同時に反転させたいということです。

どうすればいいですか?同じアニメーション ブロックの一部として、または別のブロックとしてアニメーション化するのはどのビューですか? 何かヒントをいただければ幸いです。私はまだアニメーションをうまく扱っていません。

4

3 に答える 3

5

ここでいくつかの議論がありますが、解決策はそれほどエレガントではありません。

まず、UIBarButtonItemは の子孫ではUIViewないため、 UIKit アニメーションを で直接使用することはおそらくできませんUIBarButtonItem。ただし、 を設定しcustomViewてアニメーション化することはできます。同じアニメーション ブロックを使用できます。

于 2009-07-17T00:57:14.580 に答える
2

右のナビゲーションボタンにカスタムUIViewを使用するだけで、2つのボタンを切り替えることができます。

右のナビゲーションボタンアイテムとして表示されるカスタムUIViewを作成する簡単なアプローチを使用できます。このUIViewには、切り替えたい2つのUIButtonが含まれている必要があります。UIButtonはUIViewでもあるため、通常のUIビューを反転できるのと同じトランジションを使用して反転でき、もちろんタップすることもできます。動作するサンプルコードは次のとおりです。

注:便利な関数を使用して、UIViewControllerのカスタムカテゴリとしてボタンを作成します(注:これと同じコードを追加して、UIViewのカスタムカテゴリを作成することもできます。同じ行をコピーして、UIViewControllerをUIViewに置き換えてください)-必要に応じて以下のコードを含めてカスタムカテゴリを作成するだけでも使用できます。または、通常どおりにUIButtonを作成することもできます。

// create custom category for UIViewController to allow common button creation routine, add to .m or .mm file or add to a .h file and #import that .h file
        @interface UIViewController (ButtonAndSelector)
        - (UIButton *)createUIButtonWithImage:(UIImage *)image forState:(UIControlState)state withSelector:(SEL)selector usingFrame:(CGRect)buttonImageFrame;
        @end

        @implementation UIViewController (ButtonAndSelector)
        - (UIButton *)createUIButtonWithImage:(UIImage *)buttonImage forState:(UIControlState)state withSelector:(SEL)selector usingFrame:(CGRect)buttonImageFrame
        {
            UIButton *button = [[UIButton alloc]  initWithFrame:buttonImageFrame];
            [button setBackgroundImage:buttonImage forState:state];
            [button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
            [button setShowsTouchWhenHighlighted:YES];
            return button;
        }
        @end

// add this to your .h file:
        @property (strong, nonatomic) UIView *coverListView;
        @property (strong, nonatomic) UIButton *listButton;
        @property (strong, nonatomic) UIButton *coverButton;

        - (void)animateCoverListButtonFlip;

// add this to your .m or .mm file to synthesize the variables:
        @synthesize coverListView;
        @synthesize listButton;
        @synthesize coverButton;

// add this to your .m or .mm file in the viewDidLoad:

        // setup right button bar (flips between list icon and coverart image)
        self.coverListView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 32, 30)];
        self.coverListView.backgroundColor = [UIColor clearColor];
        self.coverListView.opaque = NO;

        self.listButton = [self createUIButtonWithImage:[UIImage imageNamed:@"navbar_icon_tracklisting"] forState:UIControlStateNormal withSelector:@selector(showHideQueue) usingFrame:CGRectMake(0, 0, 32, 30)];
        self.listButton.backgroundColor = [UIColor clearColor];
        self.listButton.showsTouchWhenHighlighted = NO;

        self.coverButton = [self createUIButtonWithImage:[UIImage imageNamed:@"default_coverart_small"] forState:UIControlStateNormal withSelector:@selector(showHideQueue) usingFrame:CGRectMake(0, 0, 32, 30)];
        [self.coverListView addSubview:self.coverButton]; // show coverButton by default
            self.coverButton.showsTouchWhenHighlighted = NO;

        UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.coverListView];
        [self.navigationItem setRightBarButtonItem:barButtonItem];


// add this to viewDidAppear if you want to flip the button when the screen appears like the build in iPod app does
        [self animateCoverListButtonFlip];

// add this routine to flip the right navigation bar custom view / buttons
        - (void)animateCoverListButtonFlip
        {
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.75];    
            [UIView setAnimationTransition:([self.listButton superview] ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight) forView:self.coverListView cache:YES];

            if ([self.listButton superview]) {
                [self.listButton removeFromSuperview];
                [self.coverListView addSubview:self.coverButton];
            } else {
                [self.coverButton removeFromSuperview];
                [self.coverListView addSubview:self.listButton];
            }
            [UIView commitAnimations];
        }

// when the playing album cover changes, remember to update the coverButton:
        UIImage *artworkImage; // set this to the current playing album image
        [self.coverButton setImage:artworkImage forState:UIControlStateNormal]; 

// don't forget to call the animateCoverListButtonFlip in the button click handler (shown above as showHideQueue) that shows and hides the cover/queue(list of album tracks0 - something like this:

        - (void)showHideQueue
        {    
            [self animateCoverListButtonFlip];

            /* replace the code below that is commented out here with your own code that transitions between your cover view and your list view of album tracks, this code shows my transition and references views that are not part of this example/answer, but you don't need those - you'll have your own cover view (musicPlayerView) and list view (musicQueueView) to flip between.
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.75];
            [UIView setAnimationTransition:([musicQueueView superview] ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight) forView:self.contentView cache:YES];

            if ([musicQueueView superview]) { // if music queue is displayed
                [musicQueueView removeFromSuperview];
                [self.contentView addSubview:musicPlayerView];
            } else {
                [musicPlayerView removeFromSuperview];
                [self.contentView addSubview:musicQueueView];
                [[musicQueueView queueTableView] reloadData];
            }
            [UIView commitAnimations];*/
        }
于 2012-04-29T00:14:09.220 に答える
2

さて、これを修正するために実際に行ったことは次のとおりです。

すでにカスタム タイトル ビューを使用していました。を使用する代わりにrightBarButtonItem、カスタム ビューをより広くしました。

ボタンの両側のイメージを作成し、ナビゲーション フレームを完成させて、アプリケーションに埋め込みました。私のタイトルビューでは、次のように入力しました。

  • 適切に配置UIViewされた、右のコントロール ( と呼びます) の代わりとなるA。rightControl
  • UIViewに応答しUIControlEventTouchUpInsideてトリガーするの上のボタンflipSide:

UIImageView実行時にfor each 状態を作成します。私は両方を に入れUIImageViewましたrightControlが、デフォルトではないものを非表示にします。flipSide:専用のアニメーション ブロックで非表示のフラグを切り替えます。

めちゃめちゃ変。しかし、それは機能します。

于 2009-07-17T16:19:34.040 に答える