ios激情详解之线程运用

13 篇文章 0 订阅
#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.
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值