如何在UINavigationBar中为按钮更改设置动画?

时间:2021-11-15 20:29:57

I am calling the -(void)setEditing:(BOOL)editing animated:(BOOL)animated method in my code to swap between two buttons on the RHS of my navigation bar.

我在我的代码中调用 - (void)setEditing:(BOOL)编辑动画:(BOOL)动画方法,以便在导航栏的RHS上的两个按钮之间进行交换。

-(void)setEditing:(BOOL)editing animated:(BOOL)animated {  
  [super setEditing:editing animated:animated];  

  // Toggle ‘+’ and ‘Add To Order’ button.    

  if( editing ) {
    self.navigationItem.rightBarButtonItem = self.addItemButton;  
  }  
  else {           
    self.navigationItem.rightBarButtonItem = self.addToOrderButton;  
  } 
}

Where self.addItemButton and self.addToOrderButton are ivars containing predefined UIBarButtonItems, setup in awakefromNib.

self.addItemButton和self.addToOrderButton是包含预定义UIBarButtonItems的ivars,在awakefromNib中设置。

The button in self.addToOrderButton is significantly wider then the one in self.addItemButton, so I’d like their to be a subtle animation between the two widths when the change in editing state is triggered (by tapping a standard editButtonItem on the LHS of the navigation.

self.addToOrderButton中的按钮比self.addItemButton中的按钮宽得多,因此当触发编辑状态的更改时(通过点击LHS上的标准editButtonItem),我希望它们是两个宽度之间的微妙动画。导航。

If I surround the whole if:else with [UIView beginAnimations:nil context:NULL]; and [UIView commitAnimations]; the button change does animate, but with their positions—flying into place individually from the top left—rather than in place and just animating their widths.

如果我用[UIView beginAnimations:nil context:NULL]包围整个if:else;和[UIView commitAnimations];按钮的变化会产生动画效果,但是它们的位置会从左上角单独飞行到位,而不是就位,只是动画它们的宽度。

How I animate the navigation bar elements so that each individual one (the RHS button, the title) animate in more appropriate, restrained ways?

我如何设置导航栏元素的动画,以便每个人(RHS按钮,标题)以更恰当,更有限的方式制作动画?

1 个解决方案

#1


71  

self.navigationItem.rightBarButtonItem = self.addToOrderButton; 

There are specific methods you can use to animate the right and left bar button items:

您可以使用特定方法为右侧和左侧条形按钮项设置动画:

 [self.navigationItem setRightBarButtonItem: self.addToOrderButton animated:YES];

...which will animate it for you. If you need everything to animate (including the title) you can also use the setItems:animated: method of your navigation bar (pass it in an array of the navigation items you'd like to display)

...它会为你制作动画。如果您需要动画的所有内容(包括标题),您还可以使用导航栏的setItems:animated:方法(将其传递给您要显示的导航项目数组)

#1


71  

self.navigationItem.rightBarButtonItem = self.addToOrderButton; 

There are specific methods you can use to animate the right and left bar button items:

您可以使用特定方法为右侧和左侧条形按钮项设置动画:

 [self.navigationItem setRightBarButtonItem: self.addToOrderButton animated:YES];

...which will animate it for you. If you need everything to animate (including the title) you can also use the setItems:animated: method of your navigation bar (pass it in an array of the navigation items you'd like to display)

...它会为你制作动画。如果您需要动画的所有内容(包括标题),您还可以使用导航栏的setItems:animated:方法(将其传递给您要显示的导航项目数组)