#import "ViewController.h"
#define ScreenWidth [[UIScreen mainScreen]bounds].size.width
#define ScreenHeight [[UIScreen mainScreen]bounds].size.height
@interface ViewController ()
{
UIImageView *_fireImage;
UIButton *_pauseButton;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建火焰背景
_fireImage=[[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
_fireImage.image=[UIImage imageNamed:@"fire.png"];
[self.view addSubview:_fireImage];
// 创建火焰数组
NSMutableArray *imageArray=[[NSMutableArray alloc] init];
//for循环向数组中添加 UIImage类型的对象
for(int i=0;i<2;i++)
{
//获得图片名称
NSString *imageName=[NSString stringWithFormat:
@"loading%d.png",i];
UIImage *imageFire=[UIImage imageNamed:imageName];
[imageArray addObject:imageFire];
}
_fireImage.animationDuration=0.5;
_fireImage.animationImages=imageArray;
[_fireImage startAnimating];
_pauseButton=[UIButton buttonWithType:UIButtonTypeSystem];
_pauseButton.frame=CGRectMake(20,20,30,20);
[_pauseButton setTitle:@"暂停" forState:UIControlStateNormal
];
[_pauseButton addTarget:self action:@selector(pauseClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_pauseButton];
}
- (void)pauseClick
{
NSLog(@"调用了");
//static bool isStop=NO;
//isStop=!isStop;
// isAnimating判断当前是否正在动画 ;如果 是,停止动画;否则,开始动画
bool result=[_fireImage isAnimating];
if(result)
{
//停止动画 ;停止的时候,显示的图片是 _fireView最初赋值的图片
//与Timer区别在于,Timer停止时显示最后一张照片,而帧动画停止时显示最初赋值的照片
[_fireImage stopAnimating];
[_pauseButton setTitle:@"开始" forState:UIControlStateNormal];
}
else
{
[_fireImage startAnimating];
[_pauseButton setTitle:@"暂停" forState:UIControlStateNormal];
}
}