2012-06-05 100 views
3

如何添加动画gif文件作为UIButton的默认图像。 我已添加gif文件作为按钮图像。但它不是动画。带动画gif的IOS UIButton图像

谢谢。

+0

会[这个相关的问题的答案](http://stackoverflow.com/questions/1679011/implement-animation-in-uibutton)可能帮助你? –

+0

没有这与我的问题没有关系。它是当按钮被按下时的动画。我想将默认图像设置为动画gif。 –

回答

13

动画GIF不会在iphone动画。你应该从你的GIF添加一个UIImageView作为子视图的UIButton,提取所有图像和动画使用动画的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]; 
+0

我必须创建哪种类型的按钮,,定制? –

+0

雅你可以创建一个自定义按钮.. – Krishnabhadra

+0

没有它不工作,,按钮不是动画 –

0

在iOS中不支持.gif文件格式。所以要避免在iOS中使用

.gif文件
0

您应该探索使用UIButton的替代方案,因为它不允许进行这种定制。例如,您可以使用 UIImageView`作为按钮,并为“触摸结束”事件设置一个动作。

0

你不能只加载一个gif文件。将gif的每一帧保存到一个单独的图像文件(png或类似文件)中 - 例如,您可以使用photoshop来完成此操作,然后使用animationImages将它们加载到UIImage中并执行startAnimating。