给UIView/UIButton添加虚线边框

要给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];

你可能感兴趣的:(iOS备忘录)