一:新建一个项目工程并导入一些图片:
二:代码:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 300, 500)];
NSMutableArray * imageArray = [[NSMutableArray alloc] init];
for(int i = 0; i < 5; i++) {
NSLog(@"the i is %d",i);
// NSString *imageName = [NSString stringWithFormat:@"kid%d",i];
NSString *imageName = [NSString stringWithFormat:@"play%d",i];
NSLog(@"the name is: %@",imageName);
UIImage *image = [UIImage imageNamed:imageName];
NSLog(@"The image is %@",image);
if(image){
[imageArray addObject:image];
NSLog(@"THe array is %@", imageArray);
}
}
imageView.animationImages = imageArray;
imageView.animationDuration = 1;
imageView.animationRepeatCount = 0;
[self.view addSubview:imageView];
[imageView startAnimating];
// Do any additional setup after loading the view, typically from a nib.
}
//-(IBAction)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender:(id)sender){
// NSLog(@"this is for testing");
//}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
三:效果:
四:log:
2018-03-20 21:43:47.142213+0800 TOCAnimationa[17276:9738030] the i is 0
2018-03-20 21:43:47.142271+0800 TOCAnimationa[17276:9738030] the name is: play0
2018-03-20 21:43:47.150596+0800 TOCAnimationa[17276:9738030] The image is <UIImage: 0x1d40b67a0>, {324, 638}
2018-03-20 21:43:47.150673+0800 TOCAnimationa[17276:9738030] THe array is (
"<UIImage: 0x1d40b67a0>, {324, 638}"
)
2018-03-20 21:43:47.150682+0800 TOCAnimationa[17276:9738030] the i is 1
2018-03-20 21:43:47.150699+0800 TOCAnimationa[17276:9738030] the name is: play1
2018-03-20 21:43:47.156305+0800 TOCAnimationa[17276:9738030] The image is <UIImage: 0x1d40b6740>, {329, 667}
2018-03-20 21:43:47.156351+0800 TOCAnimationa[17276:9738030] THe array is (
"<UIImage: 0x1d40b67a0>, {324, 638}",
"<UIImage: 0x1d40b6740>, {329, 667}"
)
2018-03-20 21:43:47.156361+0800 TOCAnimationa[17276:9738030] the i is 2
2018-03-20 21:43:47.156376+0800 TOCAnimationa[17276:9738030] the name is: play2
2018-03-20 21:43:47.161902+0800 TOCAnimationa[17276:9738030] The image is <UIImage: 0x1d40b6c20>, {308, 658}
2018-03-20 21:43:47.161955+0800 TOCAnimationa[17276:9738030] THe array is (
"<UIImage: 0x1d40b67a0>, {324, 638}",
"<UIImage: 0x1d40b6740>, {329, 667}",
"<UIImage: 0x1d40b6c20>, {308, 658}"
)
2018-03-20 21:43:47.161965+0800 TOCAnimationa[17276:9738030] the i is 3
2018-03-20 21:43:47.161979+0800 TOCAnimationa[17276:9738030] the name is: play3
2018-03-20 21:43:47.167056+0800 TOCAnimationa[17276:9738030] The image is <UIImage: 0x1d40b6da0>, {303, 661}
2018-03-20 21:43:47.167110+0800 TOCAnimationa[17276:9738030] THe array is (
"<UIImage: 0x1d40b67a0>, {324, 638}",
"<UIImage: 0x1d40b6740>, {329, 667}",
"<UIImage: 0x1d40b6c20>, {308, 658}",
"<UIImage: 0x1d40b6da0>, {303, 661}"
)
2018-03-20 21:43:47.167119+0800 TOCAnimationa[17276:9738030] the i is 4
2018-03-20 21:43:47.167132+0800 TOCAnimationa[17276:9738030] the name is: play4
2018-03-20 21:43:47.171981+0800 TOCAnimationa[17276:9738030] The image is <UIImage: 0x1d40b6fe0>, {314, 670}
2018-03-20 21:43:47.172045+0800 TOCAnimationa[17276:9738030] THe array is (
"<UIImage: 0x1d40b67a0>, {324, 638}",
"<UIImage: 0x1d40b6740>, {329, 667}",
"<UIImage: 0x1d40b6c20>, {308, 658}",
"<UIImage: 0x1d40b6da0>, {303, 661}",
"<UIImage: 0x1d40b6fe0>, {314, 670}"
)