iphone - 如何为 UIBarButtonItem 设置动画

标签 iphone objective-c ipad

我想像 TheElements 示例项目那样为 UIBarButtonItem 设置动画。 我很确定我必须使用 customView 属性,但我不想使用图像来执行此操作,因为我需要使用一些本地化字符串(多语言)更改标题. 那么是否可以创建一个看起来像 UIBarButtonItemUIButton

最佳答案

这是代码。

NSArray *images = [NSArray arrayWithObjects:
        [UIImage imageNamed:@"image0.png"],
        [UIImage imageNamed:@"image1.png"],
        nil];

imageView = [[UIImageView alloc] initWithImage:[UIImage  imageNamed:@"image0.png"]];
imageView.animationImages = images;

UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = self.imageView.bounds;
[button addSubview:self.imageView];
[button addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem * barButton = [[[UIBarButtonItem alloc] initWithCustomView: button] autorelease];

一些注意事项:

UIButton 的面积为零,因为它在初始化时没有设置边界,因此边界是用 UIImageView 的边界初始化的(它有自己的边界从图像初始化)。

UIButton 处理触摸事件的 Action /目标。 UIBarButtonItem 的操作/目标未设置。

动画:

[imageView startAnimating];

关于iphone - 如何为 UIBarButtonItem 设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7514373/

相关文章:

iphone - Mobile Safari - 输入字段提交按钮

ios - 捆绑标识符和产品名称中区分大小写

ios - 使用动态菜单数据的 MF 侧菜单问题

iphone - Xcode:启用分页的 UIScrollView 中的 Pdf

iphone - 使用 'tap-to-focus' 和曝光控制的 GPUImage 视频捕获

iphone - 播放视频后更改 View

objective-c - 获取 NSString 的 floatvalue 时用点替换逗号

ios - 如何计算 UIView 相对于 iPad 上背景图像的位置

iphone - 重置所有 iOS 模拟器的内容和设置

iphone - 使用 Plist 字典进行应用程序设置