在我的iOS应用程序中,我有一个带UIToolbar的媒体播放器用于控件。当我触摸播放器屏幕时,我想让UIBarButtonItem从左侧滑动到UIToolbar上。
这是我尝试过的,它确实从左边添加了UIBarButtonItem,但是没有动画部分。
// create new button
UIBarButtonItem* b = [[UIBarButtonItem alloc] initWithTitle:@"b"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
NSMutableArray* temp = [toolbar.items mutableCopy]; // store the items from UIToolbar
NSMutableArray* newItems = [NSMutableArray arrayWithObject:b]; // add button to be on the left
[newItems addObjectsFromArray:temp]; // add the "old" items
[toolbar setItems:newItems animated:YES];
非常感谢任何形式的帮助!
答案 0 :(得分:1)
我有一个类似的问题,设计师想要在导航栏中使用那种动画。
假设您的应用不需要移动其他按钮,那么您可以这样做:
// create a UIButton instead of a toolbar button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"b" forState:UIControlStateNormal];
// Save the items *before* adding to them
NSArray *items = toolbar.items;
// Create a placeholder view to put into the toolbar while animating
UIView *placeholderView = [[UIView alloc] initWithFrame:button.bounds];
placeholderView.backgroundColor = [UIColor clearColor];
[toolbar setItems:[items arrayByAddingObject:[[UIBarButtonItem alloc] initWithCustomView:placeholderView]]
animated:NO];
// get the position that is calculated for the placeholderView which has been added to the toolbar
CGRect finalFrame = [toolbar convertRect:placeholderView.bounds fromView:placeholderView];
button.frame = CGRectMake(-1*button.bounds.size.width, finalFrame.origin.y, button.bounds.size.width, button.bounds.size.height);
[toolbar addSubview:button];
[UIView animateWithDuration:duration
animations:^{ button.frame = finalFrame; }
completion:^(BOOL finished) {
// swap the placeholderView with the button
[toolbar setItems:[items arrayByAddingObject:[[UIBarButtonItem alloc] initWithCustomView:button]]
animated:NO];
}];
如果您的应用程序 需要移动其他按钮,那么它有点棘手b / c您只需使用customView栏按钮项并获取所有这些按钮项的初始位置,将它们拉出来进入工具栏(以及项目列表之外),为它们设置动画,然后将所有内容放回原处。 (简单,对吧?)祝你好运!