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

发布于 2024-10-17 09:56:53 字数 1017 浏览 8 评论 0原文

我在代码中调用 -(void)setEditing:(BOOL)editinganimated:(BOOL)animated 方法来在导航栏右侧的两个按钮之间进行交换。

-(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.addItemButtonself.addToOrderButton 是包含预定义 UIBarButtonItems 的 ivars,在 awakefromNib 中设置。

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

如果我用 [UIView beginAnimations:nil context: 包围整个 if:else : NULL];[UIView commitAnimations]; 按钮更改确实具有动画效果,但它们的位置(从左上角单独飞入到位)而不是就位并仅对它们的宽度进行动画处理。

如何对导航栏元素进行动画处理,以便每个单独的元素(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;  
  } 
}

Where self.addItemButton and self.addToOrderButton are ivars containing predefined UIBarButtonItems, setup in 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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等待我真够勒 2024-10-24 09:56:53
self.navigationItem.rightBarButtonItem = self.addToOrderButton; 

您可以使用一些特定的方法来为右侧和左侧栏按钮项目设置动画:

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

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

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文