如何在UINavigationBar中为按钮更改设置动画? [英] How to animate a button change in UINavigationBar?

查看:85
本文介绍了如何在UINavigationBar中为按钮更改设置动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)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;  
  } 
}

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

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

self.addToOrderButton 中的按钮明显宽于 self.addItemButton ,所以我希望当触发编辑状态的变化时(通过在LHS上点击标准 editButtonItem ),它们是两个宽度之间的微妙动画导航。

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.

如果我包围整个 if:else with [UIView beginAnimations: nil context:NULL]; [UIView commitAnimations]; 按钮更改会动画,但是它们的位置会从顶部单独飞行到位左边而不是就地,只是设置它们的宽度。

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.

我如何为导航栏元素设置动画,使每个单独的元素(RHS按钮,标题)动画以更恰当,更克制的方式吃饭?

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

推荐答案


self.navigationItem.rightBarButtonItem = self.addToOrderButton; 


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

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

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

...这将为您设置动画。如果您需要动画的所有内容(包括标题),您还可以使用导航栏的 setItems:animated:方法(将其传递给您导航项的数组中) d喜欢显示)

...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)

这篇关于如何在UINavigationBar中为按钮更改设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆