2009-07-17 5 views
5

私は2つのビューの間でフリップしようとしています。それは簡単ですが、コードは以下のとおりですが、フリップを実行するために使用されたボタンを同時に反転したいと考えています。iPhoneフリップ右ボタン(iTunesのような)

トラックを再生しているときに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]; 

は(ここでは画像の反転コードがコンパイルされないことがあり、私がやろうとしたかを説明しようとする後、私はそれを追加しました。)

私はトラブルに実行している私は変更したいですナビゲーションコントローラの一番右のボタンを押すと同時に反転します。

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

答えて

5

いくつかのディスカッションhereがありますが、解決策はそれほどエレガントではありません。

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

+0

私はこれが実際には私がそれに信用を与えたよりも良いアイデアであることを認識しました。私はUIImageViewの外観を作り、ボタンのように動作するのに十分な画像をキャプチャできると思います。これは私がこれを取り除くことができることを意味します。ありがとう。 –

2

さて、ここで私は実際にこれを修正するためにやったことだ:

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

ボタンの両端の画像をナビゲーションフレームで作成し、アプリケーションに埋め込んでいます。私のタイトルビューでは、私が入れ:右の制御のための私の交換となります

  • UIViewを(rightControlそれを呼び出す)、適切に配置。
  • のボタンは、UIControlEventTouchUpInsideに応答し、私のflipSide:をトリガーします。

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

非常に奇妙です。しかし、それは動作します。

+0

これについていくつかのコードを共有できますか?似たようなもので苦闘する – arnoapp

2

右のナビゲーションボタンには、2つのボタンを含むカスタムUIViewを使用するだけです。

右ナビゲーションボタン項目として表示されるカスタムUIViewを作成する簡単な方法を使用できます。このUIViewには、2つのUIButtonを入れ替えます。UIButtonsはUIViewであることを覚えておいてください。通常のUIビューを反転することができ、もちろんタップすることができます。

注:UIViewControllerのカスタムカテゴリとしてボタンを作成するために便利な関数を使用します(注:この同じコードを追加してUIViewのカスタムカテゴリを作成することもできます)行を置き換え、UIViewControllerをUIViewに置き換え) - このコードを以下のコードを組み込んでカスタムカテゴリを作成するだけでなく、通常通りにUIButtonsを作成することもできます。

// 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];*/ 
     } 
関連する問題