美文网首页
UIBezierPath 画虚线功能

UIBezierPath 画虚线功能

作者: boundlessocean | 来源:发表于2017-11-29 14:46 被阅读41次
    API
    /**
    * pattern: 虚线的长度组成
    * count: 虚线的长度组成的个数
    * phase: 忽略多少长度
    */
    - (void)setLineDash:(nullable const CGFloat *)pattern count:(NSInteger)count phase:(CGFloat)phase;
    
    Code
    - (void)drawRect:(CGRect)rect {
        [[UIColor purpleColor] set];
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:self.center radius:80 startAngle:M_PI endAngle:2*M_PI  clockwise:YES];
        CGFloat lengths[2] = { 2, 5 };
        [path setLineDash:lengths count:2 phase:0];
        path.lineWidth = 10;
        [path stroke];
    }
    
    效果
    D152C560-105A-4DF4-9198-7964B55A13E6.png
    让虚线动起来
    Code
    - (instancetype)initWithFrame:(CGRect)frame{
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [UIColor whiteColor];
            CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
            [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
            
        }
        return self;
    }
    
    - (void)update{
        self.phase -= 0.25;
    }
    
    - (void)setPhase:(CGFloat)phase{
        _phase = phase;
        [self setNeedsDisplay];
    }
    
    - (void)drawRect:(CGRect)rect {
        
        [[UIColor purpleColor] set];
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 100, 100)];
        CGFloat lengths[2] = { 10, 5 };
        [path setLineDash:lengths count:2 phase:_phase];
        path.lineWidth = 3;
        [path stroke];
    }
    
    效果
    line.gif

    相关文章

      网友评论

          本文标题:UIBezierPath 画虚线功能

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