- (void)viewDidLoad
{
[superviewDidLoad];
// slider 控制播放速度
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(40, 250, 150, 30)];
[self.view addSubview:slider];
slider.value = 0.5;
slider.maximumValue = 3;
slider.minimumValue = 0;
// 添加事件
[slider addTarget:selfaction:@selector(speed:) forControlEvents:UIControlEventValueChanged];
// 动画 (_imageView 是全局变量)
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 40, 150, 150)];
[self.view addSubview: _imageView];
[_imageView release];
NSMutableArray *images = [NSMutableArray arrayWithCapacity:10];
for (int i = 1; i < 10; i++) {
NSString *imageName =[NSStringstringWithFormat:@"man-%d.tiff", i];
UIImage *image = [UIImage imageNamed:imageName];
[images addObject:image]; // 将图片加到图片数组中
}
// 设置动画数组
_imageView.animationImages = images;
// 设置播放时长所有图片播放的总时间
_imageView.animationDuration = slider.value;
// 设置播放次数默认无限
// _imageView.animationRepeatCount = 1000;
// 开始播放
[_imageViewstartAnimating];
}
// slider 控制动画快慢
- (void)speed:(UISlider *)slider
{
[_imageViewstopAnimating];
_imageView.animationDuration = slider.value;
[_imageViewstartAnimating];
}