// // UIView+RDSClip.m // singleProduct // // Created by coderYK on 2018/4/30. // Copyright © 2018年 RDIOT. All rights reserved. // #import "UIView+RDSClip.h" @implementation UIView (RDSClip) - (void)rds_setCornerRadiusWithType:(Corner_Radius_Or_BorderLine_TYPE)type Radius:(CGFloat)cornerRadius { self.layer.cornerRadius = 0; UIRectCorner rectcorners ; switch (type) { case Corner_Radius_Or_BorderLine_All:{ rectcorners = UIRectCornerAllCorners; self.layer.cornerRadius = cornerRadius; self.layer.borderColor = [UIColor blackColor].CGColor; } break; case Corner_Radius_Or_BorderLine_UP:{ rectcorners = UIRectCornerTopRight|UIRectCornerTopLeft; } break; case Corner_Radius_Or_BorderLine_DOWN:{ rectcorners = UIRectCornerBottomLeft|UIRectCornerBottomRight; } break; case Corner_Radius_Or_BorderLine_LEFT:{ rectcorners = UIRectCornerTopLeft|UIRectCornerBottomLeft; } break; case Corner_Radius_Or_BorderLine_RIGHT:{ rectcorners = UIRectCornerTopRight|UIRectCornerBottomRight; } break; } UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:rectcorners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; self.layer.masksToBounds = YES; } - (void)rds_drawBorderLineWithType:(Corner_Radius_Or_BorderLine_TYPE)type lineWidth:(CGFloat)lineWidth lineColor:(UIColor *)lineColor { /// 线的路径 UIBezierPath *bezierPath = [UIBezierPath bezierPath]; if (type==Corner_Radius_Or_BorderLine_All) { self.layer.borderWidth = lineWidth; self.layer.borderColor = lineColor.CGColor; return; } else if (type==Corner_Radius_Or_BorderLine_UP) { [bezierPath moveToPoint:CGPointMake(0.0f, lineWidth)]; [bezierPath addLineToPoint:CGPointMake(self.frame.size.width, lineWidth)]; } else if (type==Corner_Radius_Or_BorderLine_RIGHT) { [bezierPath moveToPoint:CGPointMake(self.frame.size.width-lineWidth, 0.0f)]; [bezierPath addLineToPoint:CGPointMake( self.frame.size.width-lineWidth, self.frame.size.height)]; } else if (type==Corner_Radius_Or_BorderLine_DOWN) { [bezierPath moveToPoint:CGPointMake(0.0f, self.frame.size.height-lineWidth)]; [bezierPath addLineToPoint:CGPointMake( self.frame.size.width, self.frame.size.height-lineWidth)]; } else if (type==Corner_Radius_Or_BorderLine_LEFT) { [bezierPath moveToPoint:CGPointMake(0.0f+lineWidth, self.frame.size.height)]; [bezierPath addLineToPoint:CGPointMake(0.0f+lineWidth, 0.0f)]; } CAShapeLayer * shapeLayer = [CAShapeLayer layer]; shapeLayer.strokeColor = lineColor.CGColor; shapeLayer.fillColor = [UIColor clearColor].CGColor; /// 添加路径 shapeLayer.path = bezierPath.CGPath; /// 线宽度 shapeLayer.lineWidth = lineWidth; [self.layer insertSublayer:shapeLayer atIndex:0]; self.layer.masksToBounds = YES; } - (void)rds_setCornerRadius:(CGFloat)cornerRadius borderLineWidth:(CGFloat)lineWidth lineColor:(UIColor*)lineColor{ [self.layer setMasksToBounds:YES]; [self.layer setCornerRadius:cornerRadius]; //设置矩形四个圆角半径 //边框宽度 [self.layer setBorderWidth:lineWidth]; //设置边框颜色有两种方法:第一种如下: //CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); //CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 0, 0, 0, 1 }); //[self.layer setBorderColor:colorref];//边框颜色 //第二种方法如下: self.layer.borderColor = lineColor.CGColor; } - (void)rds_addShadowAndCornerRadius:(CGFloat)cornerRadius shadowColor:(UIColor *)shadowColor { [self rds_addShadowAndCornerRadius:cornerRadius shadowOpacity:0.85f shadowRadius:1 shadowColor:shadowColor]; } - (void)rds_addShadowAndCornerRadius:(CGFloat)cornerRadius shadowOpacity:(float)shadowOpacity shadowRadius:(CGFloat)shadowRadius shadowColor:(UIColor *)shadowColor { self.layer.cornerRadius = cornerRadius; UIBezierPath *roundPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:cornerRadius]; self.layer.shadowColor = shadowColor == nil ? [UIColor blackColor].CGColor : shadowColor.CGColor; self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f); self.layer.shadowOpacity = shadowOpacity; self.layer.shadowRadius = shadowRadius; self.layer.shadowPath = roundPath.CGPath; } - (void)rds_addCornerRadius:(CGFloat)cornerRadius { UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:cornerRadius]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; self.layer.masksToBounds = YES; } @end