CALayer进阶

相比大家对CALayer并不陌生,下面是我自己对CALayer做的用法总结:

/**
 *  添加圆角
 *  @param view 需要添加圆角的View
 */
- (void)addRoundCornerToView:(UIView *)view{
    view.layer.cornerRadius = 20;//圆角的程度,当值为view.size.height/2的时候,图形变成圆
    view.layer.masksToBounds = YES;
   // view.layer.borderWidth = 3;
    //view.layer.borderColor = [UIColor yellowColor].CGColor;
}
/**
 *  添加虚线边框
 *  @param view 需要添加虚线边框的View
 */
- (void)addLineLayerToView:(UIView *)view{
    CAShapeLayer * border = [CAShapeLayer layer];
    border.path = [UIBezierPath bezierPathWithRect:view.bounds].CGPath;
//虚线的路径,如果加了圆角的话,可以使用贝塞尔另一个方法获得圆角路径
    border.frame = view.bounds;
    border.strokeColor = [[UIColor redColor] CGColor];
    border.fillColor = nil;
    border.lineDashPattern = @[@6, @6];
    [view.layer addSublayer:border];
}

对于圆角的奇葩需求:部分圆角,并不是四个角都是圆角,可能其中的某几个进行圆角处理,利用mask就可以达到效果。

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_imageView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(20, 20)];//这里的参数分别是设置某一个圆角个圆角度数
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];    maskLayer.frame = _imageView.bounds;
maskLayer.path = maskPath.CGPath;
_imageView.layer.mask = maskLayer;```
未完待续...



欢迎关注我的[微博](http://dwz.cn/Smallwolf)和[博客](http://blog.sina.com.cn/u/2209572342)

你可能感兴趣的:(CALayer进阶)