裁剪矩形指定圆角 2018-01-22 | Skills 某UI中经常会有裁剪某两个矩形圆角的需求, 本文提供两个简单的裁剪矩形指定圆角的方法. OC写法 1234567891011121314/** 裁剪某几个圆角 @param view 被裁减的view @param corners 裁剪的角 @param cornerRadii 裁剪半径 */ - (void)clipCornerOf:(UIView *)view byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)radii{ UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: view.bounds byRoundingCorners: corners cornerRadii: radii]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bgView.bounds; maskLayer.path = maskPath.CGPath; view.layer.mask = maskLayer; } Swift写法 12345678910111213/** 裁剪某几个圆角 @param view 被裁减的view @param corners 裁剪的角 @par func clipCorner(of view: UIView, corners: UIRectCorner, radii:CGSize) -> Void { let path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: corners, cornerRadii: radii) let shapeLayer = CAShapeLayer() shapeLayer.frame = view.bounds shapeLayer.path = path.cgPath view.layer.mask = shapeLayer } Swift分类写法(OC分类可以自行编写) 123456789extension UIView{ func clip(corners: UIRectCorner, radii:CGSize) -> Void { let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: radii) let shapeLayer = CAShapeLayer() shapeLayer.frame = bounds shapeLayer.path = path.cgPath layer.mask = shapeLayer } }