iPhone SDK:将动画加载(齿轮)图像设置为 UIBarButtonItem

发布于 2024-08-18 10:54:00 字数 169 浏览 4 评论 0原文

有没有办法将 Apple 旋转齿轮等动画图像设置为 UIBarButtonItem?

我已经尝试过这行代码,但动画 gif 图像无法播放:

myButton.image = [UIImage imageNamed:@"spinningGear.gif"];

Is there any way to set an animated image like the Apple spinning gear to an UIBarButtonItem?

I have tried this line of code but the animated gif image wont play:

myButton.image = [UIImage imageNamed:@"spinningGear.gif"];

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

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

发布评论

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

评论(4

拒绝两难 2024-08-25 10:54:00

尝试创建一个 UIActivityIndi​​catorView 并使用 -[UIBarButtonItem initWithCustomView:] 将其分配给您的按钮。

Try creating a UIActivityIndicatorView and assigning it to your button with -[UIBarButtonItem initWithCustomView:].

空城旧梦 2024-08-25 10:54:00

我认为 UIBarButtonItem 不可能做到这一点。

您可能希望使用 customView (属性或 initWithCustomView)并使用 UIImageView 作为该视图。这仍然不会使 gif 动画“开箱即用”(刚刚选中)。

如果这样做,您有两个选择:

  • 使用 UIImageView 类中的 animatedImages 并为每个帧使用单独的图像(从头写出 - 代码可能会有一些错误):

NSMutableArray * imgs = [NSMutableArray array];
for(int i = 0; i < NUMBER_OF_FRAMES; i++) {
    [imgs addObject: [UIImage imageNamed: [NSString stingWithFormat: @"anim%d.png", i]]];
}
UIImageView * imgview = [[UIImageView alloc] init];
imgview.animatedImages = imgs;
[imgview startAnimating];

I don't think that's possible with UIBarButtonItem.

You might want to use customView for that (the property or initWithCustomView) and use UIImageView as that view. That still won't animate gif's "out of the box" (just checked).

If you do so you have two options:

  • use animatedImages from UIImageView class and use separate images for every frame (writing out of head - code might have some errors):

NSMutableArray * imgs = [NSMutableArray array];
for(int i = 0; i < NUMBER_OF_FRAMES; i++) {
    [imgs addObject: [UIImage imageNamed: [NSString stingWithFormat: @"anim%d.png", i]]];
}
UIImageView * imgview = [[UIImageView alloc] init];
imgview.animatedImages = imgs;
[imgview startAnimating];

幸福不弃 2024-08-25 10:54:00

我发现这行是不正确的:

[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];

...Apple 的默认刷新按钮的实际大小有点不同。如果您有其他项目在该工具栏上进行自动布局,则需要获得正确的大小。

不幸的是,Apple没有提供 API 来查找大小。经过反复试验,这似乎是正确的:

[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 28, 28)];

I found that this line was incorrect:

[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];

...the actual size of the default Refresh button from Apple is a little different. If you have other items doing auto-layout on that toolbar, you need to get the size right.

Unfortunately, Apple provides no API for finding out the size. By trial and error, it seems this is correct:

[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 28, 28)];
誰ツ都不明白 2024-08-25 10:54:00

我通过将 UIBarButtonItem 的 customView 设置为带有图标图像的 UIButton,然后添加 UIActivityIndi​​cator 作为 UIButton 的子视图来完成此操作。

为了进行设置,我只需将 UIButton 拖到 Interface Builder 中的 UIBarButtonItem 上(您也可以在代码中执行此操作)。然后显示活动指示器:

UIButton *customButton = (UIButton *)self.refreshButton.customView;
[customButton setImage:nil forState:UIControlStateNormal];
[customButton removeTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[customButton addTarget:self action:@selector(altButtonAction) forControlEvents:UIControlEventTouchUpInside];
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
self.activityIndicator.frame = CGRectMake(round((customButton.frame.size.width - 25) / 2), round((customButton.frame.size.height - 25) / 2), 25, 25);
self.activityIndicator.userInteractionEnabled = FALSE; // this allows the button to remain tappable
[customButton addSubview:self.activityIndicator];
[self.activityIndicator startAnimating];

并返回到默认按钮状态:

UIButton *customButton = (UIButton *)self.refreshButton.customView;
[customButton setImage:[UIImage imageNamed:@"IconRefresh"] forState:UIControlStateNormal];
[customButton removeTarget:self action:@selector(altButtonAction) forControlEvents:UIControlEventTouchUpInside];
[customButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[self.activityIndicator removeFromSuperview];
[self.activityIndicator release];

一些注意事项:

  1. 如果您不想在按钮处于“活动”状态时更改按钮操作,则可以删除 addTarget 和removeTarget 行。
  2. 如果您不希望按钮在处于“活动”状态时可点击,则可以省略活动指示器的 userInteractionEnabled 行(或删除目标并重新添加它)。
  3. 具有 customView 的 UIBarButtonItem 将不会显示按钮边框。如果您想要此边框,则必须制作自己的图像并将其添加为 UIButton 的背景图像。

I did this by setting the customView of the UIBarButtonItem to a UIButton with an icon image, then adding a UIActivityIndicator as a subview of the UIButton.

To set it up, I just dragged a UIButton onto the UIBarButtonItem in Interface Builder (you could also do that in your code). Then to display the activity indicator:

UIButton *customButton = (UIButton *)self.refreshButton.customView;
[customButton setImage:nil forState:UIControlStateNormal];
[customButton removeTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[customButton addTarget:self action:@selector(altButtonAction) forControlEvents:UIControlEventTouchUpInside];
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
self.activityIndicator.frame = CGRectMake(round((customButton.frame.size.width - 25) / 2), round((customButton.frame.size.height - 25) / 2), 25, 25);
self.activityIndicator.userInteractionEnabled = FALSE; // this allows the button to remain tappable
[customButton addSubview:self.activityIndicator];
[self.activityIndicator startAnimating];

And to return to the default button state:

UIButton *customButton = (UIButton *)self.refreshButton.customView;
[customButton setImage:[UIImage imageNamed:@"IconRefresh"] forState:UIControlStateNormal];
[customButton removeTarget:self action:@selector(altButtonAction) forControlEvents:UIControlEventTouchUpInside];
[customButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[self.activityIndicator removeFromSuperview];
[self.activityIndicator release];

A few notes:

  1. If you don't want to change the button action when it's in the "active" state, you can remove the addTarget and removeTarget lines.
  2. If you don't want the button to be tappable when it's in the "active" state, you can leave out the userInteractionEnabled line for the activity indicator (or remove the target and re-add it).
  3. A UIBarButtonItem with a customView will not display the button border. If you want this border, you'll have to make your own image and add it as the background image of the UIButton.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文