给UIView/UIButton添加虚线边框

版权声明:本文为博主原创文章,未经博主允许不得转载。个人独立博客:https://ichenwin.github.io GitHub、知乎等网站用户名:iChenwin https://blog.csdn.net/u013993802/article/details/72626190

要给UIButton等视图加一圈虚线边框,这里是其中一种方法,就是在原来的视图的layer上再添加一层CAShapeLayer,在这一层中使用贝塞尔曲线UIBezierPathlineDashPattern创建虚线边框。

UIView *view          = [[UIView alloc] init];
CAShapeLayer *layer   = [[CAShapeLayer alloc] init];
layer.frame            = CGRectMake(0, 0 , view.frame.size.width, view.frame.size.height);
layer.backgroundColor   = [UIColor clearColor].CGColor;

UIBezierPath *path    = [UIBezierPath bezierPathWithRoundedRect:layer.frame cornerRadius:4.0f];
layer.path             = path.CGPath;
layer.lineWidth         = 4.0f;
layer.lineDashPattern    = @[@4, @4];
layer.fillColor          = [UIColor clearColor].CGColor;
layer.strokeColor       = [UIColor whiteColor].CGColor;

[view.layer addSublayer:layer];

猜你喜欢

转载自blog.csdn.net/u013993802/article/details/72626190