#import "RootViewController.h"
@interface RootViewController ()
@property (nonatomic, retain)UIImageView *imageView;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addSubViews];
}
// 创建一个imageView加载图片 两个按钮
// 首先去声明一下imageView
- (void)addSubViews
{
self.imageView = [[UIImageView alloc] initWithFrame:(CGRectMake(40, 80, 300, 400))];
self.imageView.backgroundColor = [UIColor blueColor];
[self.view addSubview:self.imageView];
// 播放一组图片
NSMutableArray *imageArray = [NSMutableArray array];
// 循环添加图片
for (int i = 0; i < 5; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];
// 图片加入数组
[imageArray addObject:image];
}
// 设置播放的属性
self.imageView.animationDuration = 2;
self.imageView.animationImages = imageArray;
self.imageView.animationRepeatCount = 0;
// 需求1,点击按钮开始播放 再点击停止播放
UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
button.frame = CGRectMake(80, 500, 40, 50);
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(actionButton:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
// 需求二 点击另一按钮 循环打印0 -- 88888
UIButton *button1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
button1.frame = CGRectMake(240, 500, 40, 50);
button1.backgroundColor = [UIColor redColor];
[button1 addTarget:self action:@selector(actionButton1) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button1];
// 实现一边打印一边开启动画,也就是让子线程去帮忙完成
// 通过NSThread轻量化的线程 可以开启线程终止线程 也即是达到再点击停止播放效果
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(actionButton1) object:nil];
// 开启子线程
[thread start];
}
- (void)actionButton:(UIButton *)button
{
if ([self.imageView isAnimating] == NO) {
[self.imageView startAnimating];
} else {
[self.imageView stopAnimating];
}
}
- (void)actionButton1
{
// 添加一个自动释放池
@autoreleasepool {
for (int i = 0; i < 888888; i++) {
NSLog(@"%d", i);
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}