iPhone SDK:将动画加载(齿轮)图像设置为 UIBarButtonItem
有没有办法将 Apple 旋转齿轮等动画图像设置为 UIBarButtonItem?
我已经尝试过这行代码,但动画 gif 图像无法播放:
myButton.image = [UIImage imageNamed:@"spinningGear.gif"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试创建一个
UIActivityIndicatorView
并使用-[UIBarButtonItem initWithCustomView:]
将其分配给您的按钮。Try creating a
UIActivityIndicatorView
and assigning it to your button with-[UIBarButtonItem initWithCustomView:]
.我认为 UIBarButtonItem 不可能做到这一点。
您可能希望使用
customView
(属性或initWithCustomView
)并使用UIImageView
作为该视图。这仍然不会使 gif 动画“开箱即用”(刚刚选中)。如果这样做,您有两个选择:
animatedImages
并为每个帧使用单独的图像(从头写出 - 代码可能会有一些错误):UIImageView
的类 http://blog.stijnspijker.nl/2009/07/animated-and-transparent-gifs-for-iphone-made-easyI don't think that's possible with UIBarButtonItem.
You might want to use
customView
for that (the property orinitWithCustomView
) and useUIImageView
as that view. That still won't animate gif's "out of the box" (just checked).If you do so you have two options:
animatedImages
from UIImageView class and use separate images for every frame (writing out of head - code might have some errors):UIImageView
out of gif images from this here http://blog.stijnspijker.nl/2009/07/animated-and-transparent-gifs-for-iphone-made-easy我发现这行是不正确的:
...Apple 的默认刷新按钮的实际大小有点不同。如果您有其他项目在该工具栏上进行自动布局,则需要获得正确的大小。
不幸的是,Apple没有提供 API 来查找大小。经过反复试验,这似乎是正确的:
I found that this line was incorrect:
...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:
我通过将 UIBarButtonItem 的 customView 设置为带有图标图像的 UIButton,然后添加 UIActivityIndicator 作为 UIButton 的子视图来完成此操作。
为了进行设置,我只需将 UIButton 拖到 Interface Builder 中的 UIBarButtonItem 上(您也可以在代码中执行此操作)。然后显示活动指示器:
并返回到默认按钮状态:
一些注意事项:
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:
And to return to the default button state:
A few notes: