您的位置:首页 > 移动开发 > Objective-C

Objective-c View添加虚线

2016-04-26 00:00 260 查看
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGFloat viewWidth = 200;
CGFloat viewHeight = 200;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake((screenSize.width - viewWidth)/2, (screenSize.height - viewHeight) / 2, viewWidth, viewHeight)];
view.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
view.layer.cornerRadius = CGRectGetWidth(view.bounds)/2;
CAShapeLayer *borderLayer = [CAShapeLayer layer];
borderLayer.bounds = CGRectMake(0, 0, viewWidth, viewHeight);
borderLayer.position = CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds));

// borderLayer.path = [UIBezierPath bezierPathWithRect:borderLayer.bounds].CGPath;
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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: