ios – 如何动画UIBarButtonItem从左侧滑动UIToolbar?

ios – 如何动画UIBarButtonItem从左侧滑动UIToolbar?,第1张

概述在我的iOS应用程序中,我有一个带UIToolbar的媒体播放器用于控件.当我触摸播放器屏幕时,我想让UIBarButtonItem从左侧滑动到UIToolbar上. 这是我尝试过的,它确实从左边添加了UIBarButtonItem,但是没有动画部分. // create new button UIBarButtonItem* b = [[UIBarButtonItem alloc] init 在我的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];

任何形式的帮助都非常感谢!

解决方法 我有一个类似的问题,设计师想要在导航栏中使用那种动画.

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

// 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栏按钮项并获取所有这些项的初始位置,将它们拉入工具栏(并列出的项目),动画他们,然后把一切都放回去. (简单,对吧?)祝你好运!

总结

以上是内存溢出为你收集整理的ios – 如何动画UIBarButtonItem从左侧滑动UIToolbar?全部内容,希望文章能够帮你解决ios – 如何动画UIBarButtonItem从左侧滑动UIToolbar?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1022196.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存