如何将动态gif文件添加为UIButton的默认图像。
我已经将gif文件添加为按钮图像。但它没有动画效果。
谢谢。
谢谢。
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];
你不能直接加载gif文件。需要将gif的每一帧保存为单独的图像文件(png或类似格式)- 例如,你可以使用Photoshop完成此操作,然后使用animationImages将它们加载到UIImage中,并执行startAnimating。
你应该探索使用UIButton
以外的替代方案,因为它不允许进行这样的自定义。例如,你可以使用 UIImageView
来创建你的按钮,并为"触摸结束"事件设置一个操作。