Objective-c View添加虛線

 CGSize screenSize = [UIScreen mainScreen].bounds.size;spa

    CGFloat viewWidth = 200;.net

    CGFloat viewHeight = 200;3d

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake((screenSize.width - viewWidth)/2, (screenSize.height - viewHeight) / 2, viewWidth, viewHeight)];orm

    view.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];get

    view.layer.cornerRadius = CGRectGetWidth(view.bounds)/2;it

    CAShapeLayer *borderLayer = [CAShapeLayer layer];io

    borderLayer.bounds = CGRectMake(0, 0, viewWidth, viewHeight);class

    borderLayer.position = CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds));margin

    

    //    borderLayer.path = [UIBezierPath bezierPathWithRect:borderLayer.bounds].CGPath;top

    borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:borderLayer.bounds cornerRadius:CGRectGetWidth(borderLayer.bounds)/2].CGPath;

    borderLayer.lineWidth = 1. / [[UIScreen mainScreen] scale];

    //虛線邊框

    borderLayer.lineDashPattern = @[ @8 , @8];

    //實線邊框

    //    borderLayer.lineDashPattern = nil;

    borderLayer.fillColor = [UIColor clearColor].CGColor;

    borderLayer.strokeColor = [UIColor redColor].CGColor;

    [view.layer addSublayer:borderLayer];

    

    [self.view addSubview:view];

相關文章
相關標籤/搜索