2009-07-17 68 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];*/ 
     }