2009-07-17 65 views
5

我試圖在兩個視圖之間翻轉。這很簡單,代碼在下面,但我也想同時翻轉用於執行翻轉的按鈕。iPhone翻轉右鍵(如iTunes)

當您播放音軌時,您可以在iPod應用程序中看到此行爲;點擊翻蓋按鈕在封面藝術和軌道目錄之間翻轉,但它同時翻轉按鈕。

這是導航控制器上的一個頁面,我想翻轉的按鈕是rightBarButtonItem

下面是我到目前爲止的代碼。這翻轉了看法,但不是正確的巴爾頓。

[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,但該解決方案並不優雅。

首先,因爲UIBarButtonItem不是UIView的後代,所以您可能無法直接在UIBarButtonItem上使用UIKit動畫。不過,您可以嘗試設置customView並對其進行動畫處理。您可以使用相同的動畫塊。

+0

過夜我意識到這實際上是一個比我更好的主意。我想我可以捕捉到足夠的圖像,使UIImageView看起來像一個按鈕,這意味着我可以把它關閉。謝謝。 – 2009-07-17 12:51:11

2

好的,這裏是我的實際做法來解決這個問題:

我已經在使用自定義標題視圖。我沒有使用rightBarButtonItem,而是使自定義視圖變寬。

我創建了一個按鈕兩側的圖像,包含導航框架,並將它們嵌入到應用程序中。在我的標題來看,我把:

  • 一個UIView,這將是我的替代正確的控制(稱之爲rightControl),適當定位。
  • UIView上的一個按鈕,它響應UIControlEventTouchUpInside並觸發我的flipSide:

在運行時,我爲每個狀態創建一個UIImageView。我在rightControl中放置了UIImageView s,但隱藏了不是默認的那個。我在專用動畫塊中切換flipSide:的隱藏標誌。

瘋狂怪異。但它的工作。

+0

你可以分享一些代碼嗎?掙扎着類似的東西 – arnoapp 2014-11-10 17:05:41

2

只需使用包含兩個按鈕之間翻轉的右側導航按鈕的自定義UIView。

您可以使用直接創建自定義UIView的方法,將其顯示爲正確的導航按鈕項目。這個UIView應該包含你想要翻轉的兩個UIButton。請記住,UIButtons也是UIViews,所以它們可以使用相同的轉換進行翻轉,以便正常的UI視圖可以翻轉,當然它們可以被輕敲!下面是一些示例代碼的工作原理:

注意:我使用便捷函數來創建按鈕作爲UIViewController的自定義類別(注意:您可以添加相同的代碼來爲UIView創建自定義類別,只需複製相同的並用UIView替換UIViewController) - 如果您也想使用它,只需通過在下面包含以下代碼創建一個自定義類別,或者您可以像平常一樣創建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];*/ 
     }