如何为 UINavigationBar 中的按钮更改设置动画?
我在代码中调用 -(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.addItemButton
和 self.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用一些特定的方法来为右侧和左侧栏按钮项目设置动画:
...这将为您设置动画。如果您需要为所有内容设置动画(包括标题),您还可以使用导航栏的
setItems:animated:
方法(将其传递到您想要显示的导航项的数组中)There are specific methods you can use to animate the right and left bar button items:
...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)