#import “ViewController.h"
@interface ViewController ()
{
UIImageView *animationView;
NSMutableArray *imageList;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
[self loadData];
// UIImage *image =[UIImage imageNamed:@"a-1.tiff"];//还未放入图片
animationView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
animationView.animationImages = imageList;
// 设置UIImageView动态图数组:
animationView.animationDuration = 1; // 播放一组动画需要的时间
animationView.animationRepeatCount = -1; // 是否需要重复播放
[self.view addSubview:animationView];
[animationView startAnimating]; // 开始动画
[animationView stopAnimating]; // 结束动画
// animationView.isAnimating //判断是否正在播放
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10, 20, 50, 35);
[button setTitle:@"播放" forState:UIControlStateNormal];
[button setTitle:@"暂停" forState:UIControlStateSelected];
[button addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
//加载数据的方法
- (void)loadData{
imageList = [NSMutableArray array];
// 当调试的时候 打印数组的值 是nil或者null 意味着 咋们没有初始化对象 或者赋值为空
for (int i = 1; i <= 33; i++) {
NSString *imageName = [NSString stringWithFormat:@“dog.jpg-%d(被拖移).tiff”,i];//加载动画图片
[imageList addObject:[UIImage imageNamed:imageName]];
}
NSLog(@"%@",imageList);
}
- (void)play:(UIButton *)sender
{
if (sender.selected != YES) {
// 播放动画 重制按钮的title
sender.selected = YES;
[animationView startAnimating];
[self moveAnimation];
}else{
sender.selected = NO;
[animationView startAnimating];
}
}
- (void)moveAnimation
{
//设置动画的持续时间和位置
// [UIView animateWithDuration:5 animations:
// ^{
// animationView.frame = CGRectMake(300, 400, self.view.bounds.size.width, self.view.bounds.size.height) ;
// }];
[UIView animateWithDuration:5 animations:^{
animationView.frame = CGRectMake(0, 400, self.view.bounds.size.width*2, self.view.bounds.size.height*2) ;
} completion:^(BOOL finished) {
[UIView animateWithDuration:5 animations:^{
animationView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) ;
}];
}];
}