美文网首页
多线程中使用GCD的一个例子

多线程中使用GCD的一个例子

作者: yeshenlong520 | 来源:发表于2016-02-23 16:32 被阅读70次

    使用GCD编程记住一件事 就可以一招鲜吃遍天了. 刷新界面的代码 放在这个block中执行dispatch_async(dispatch_get_main_queue(),^{

    });

    对于那些要执行起来耗时间的任务应该放到其他的执行线程中 不要取阻塞主线程的执行

    dispatch_async(dispatch_get_global_queue(0,0),^{

    });

    封装工具类代码:

    import<UIKit/UIKit.h>

    import@interface HJTUtility : NSObject

    //产生一个速度的随机数

    +(int) randomIntBetweenMin:(int) min Max:(int) max;

    //生成一个随机的颜色

    +(UIColor *)randomColor;

    //生成一个随机颜色

    +(UIColor *)randomColorWithAlpha:(CGFloat) alpha;

    @end


    import "HJTUtility.h"

    @implementation HJTUtility

    +(int)randomIntBetweenMin:(int)min Max:(int)max{

    return arc4random() % (max - min + 1) + min;

    }

    +(UIColor *)randomColorWithAlpha:(CGFloat)alpha{

    //利用随机数产生随机颜色

    double red = [self randomIntBetweenMin:0 Max:255]/255.0;

    double green = [self randomIntBetweenMin:0 Max:255]/255.0;

    double blue = [self randomIntBetweenMin:0 Max:255]/255.0;

    //alpha 是透明度

    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];

    }

    +(UIColor *)randomColor{

    return [self randomColorWithAlpha:1];

    }

    @end


    建立一个汽车模型 遵循KVC:

    import<UIKit/UIKit.h>

    //个人建议使用这个定义字面常量 而不是使用#define 因为我容易弄错 呵呵

    static const int CAR_WIDTH = 30;

    static const int CAR_HEITH = 40;

    @interface HJTCar : NSObject

    //汽车左上角横坐标

    @property (nonatomic,assign) NSUInteger x;

    //汽车左上角纵坐标

    @property (nonatomic,assign) NSUInteger y;

    //汽车速度

    @property (nonatomic,assign) NSUInteger speed;

    //和汽车关联的视图

    @property (nonatomic,strong) UIView *view;

    //初始化汽车左上角的横纵坐标

    -(instancetype) initWithX:(int) x Y:(int)y;

    //汽车行驶

    -(void) carRun;

    //将汽车视图呈现在父视图上的方法

    -(void) draw:(UIView *)parentView;

    @end


    import "HJTCar.h"

    import "HJTUtility.h"

    @implementation HJTCar

    -(instancetype)initWithX:(int)x Y:(int)y{

    if (self = [super init]) {

    _x = x;

    _y = y;

    _speed = [HJTUtility randomIntBetweenMin:5 Max:9];

    _view = [[UIView alloc]initWithFrame:CGRectMake(_x, _y, CAR_WIDTH, CAR_HEITH)];

    _view.backgroundColor = [HJTUtility randomColor];

    }

    return self;

    }

    -(void)carRun{

    while (_y + CAR_HEITH < 650) {

    _y += _speed;

    CGRect rect = _view.frame;

    rect.origin.y = _y;

    //苹果官方建议:刷新视图的工作 要回到 主线程完成

    dispatch_async(dispatch_get_main_queue(), ^{

    _view.frame = rect;

    });

    //休眠50ms --可以产生20帧(流畅的动画效果)

    //区别:sleep()--单位是秒 usleep()--单位是毫秒

    usleep(50000);

    }

    }

    -(void)draw:(UIView *)parentView{

    [parentView addSubview:_view];

    }

    @end


    下面是UIViewController中的实现

    import<UIKit/UIKit.h>

    @interface ViewController : UIViewController

    @end


    import "ViewController.h"

    import "HJTCar.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    • (void)viewDidLoad {

    [super viewDidLoad];

    for (int i = 0; i < 5; i++) {

    HJTCar *car = [[HJTCar alloc]initWithX:20 + 80 * i Y:10];

    [car draw:self.view];

    // 把程序中会出现卡顿的地方 放到一个并发队列中执行 避免程序出现卡顿 假死

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

    [car carRun];

    });

    }

    }
    @end

    相关文章

      网友评论

          本文标题:多线程中使用GCD的一个例子

          本文链接:https://www.haomeiwen.com/subject/csnbkttx.html