CALayer子类一、CAShapeLayer

2018-02-08  本文已影响117人  Carson_Zhu
CAShapeLayer的属性
UIBezierPath* bezierPath_rect = [UIBezierPath bezierPathWithRect:CGRectMake(30, 50, 100, 100)];
CAShapeLayer* shapeLayer = [CAShapeLayer layer];
shapeLayer.path = bezierPath_rect.CGPath;
shapeLayer.fillColor = [UIColor clearColor].CGColor;
shapeLayer.strokeColor = [UIColor blackColor].CGColor;
shapeLayer.lineWidth = 1;
shapeLayer.lineDashPattern = @[@6, @6]; //设置虚线样式
[self.view.layer addSublayer:shapeLayer];

// 创建子线程队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 使用之前创建的队列来创建定时器,注意定时器的生命周期
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC, 0.1 * NSEC_PER_SEC);
dispatch_source_set_event_handler(self.timer, ^{
    dispatch_async(dispatch_get_main_queue(), ^{
        // 改变虚线起点
        CGFloat add = 3;
        shapeLayer.lineDashPhase -= add;
    });
});
// 开启定时器
dispatch_resume(self.timer);

运行效果:

上一篇下一篇

猜你喜欢

热点阅读