iOS带有动态gif图片的UIButton

3
如何将动态gif文件添加为UIButton的默认图像。 我已经将gif文件添加为按钮图像。但它没有动画效果。
谢谢。

这个相关问题的答案(https://dev59.com/30rSa4cB1Zd3GeqPTyXF)可能会对你有所帮助吗? - Michael Dautermann
不,这与我的问题无关。这是按钮按下时的动画。我想将默认图像设置为动画GIF。 - Shehzad Bilal
4个回答

16

iPhone上的动画GIF无法播放。您应该将UIImageView添加为UIButton的子视图,提取GIF中的所有图像,并使用UIImageView动画来播放。

UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
animatedImageView.animationImages = [NSArray arrayWithObjects:    
                               [UIImage imageNamed:@"image1.gif"],
                               [UIImage imageNamed:@"image2.gif"],
                               [UIImage imageNamed:@"image3.gif"],
                               [UIImage imageNamed:@"image4.gif"], nil];
animatedImageView.animationDuration = 1.0f;
animatedImageView.animationRepeatCount = 0;
[animatedImageView startAnimating];
[yourButton addSubview: animatedImageView];

我需要创建哪种类型的按钮,自定义的吗? - Shehzad Bilal
你需要将所有gif格式的图片作为单独的文件添加到Xcode bundle中。 - Krishnabhadra
让我们在聊天中继续这个讨论 - Krishnabhadra

1

你不能直接加载gif文件。需要将gif的每一帧保存为单独的图像文件(png或类似格式)- 例如,你可以使用Photoshop完成此操作,然后使用animationImages将它们加载到UIImage中,并执行startAnimating。


0
在iOS中不支持.gif文件格式,因此请避免在iOS中使用.gif文件。

0

你应该探索使用UIButton以外的替代方案,因为它不允许进行这样的自定义。例如,你可以使用 UIImageView 来创建你的按钮,并为"触摸结束"事件设置一个操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接