如何使UIBarButtonItem在左侧的UIToolbar上滑动?

问题描述:

在我的iOS应用程序中,我有一个UIToolbar控件的媒体播放器。我想让UIBarButtonItem从左侧滑入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]; 

任何形式的帮助,高度赞赏!

我有一个类似的问题,设计师想要在导航栏中的那种动画。

假设您的应用程序并不需要其他的按钮来移动,那么你可以做这样的:

// 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栏按钮项目并获取所有这些项目的初始位置,将它们拖入工具栏(并从项目列表中除外),为它们设置动画,然后将所有内容都放回原处。 (简单,对吧?)祝你好运!

+0

谢谢!这是一个非常好的方法!我以类似的方式解决了这个问题。但是,我杀死了UIToolbar并在其上创建了自己的带有UIButton的自定义工具栏... –