主要说一下竖线与语法的改动
下面的例子都是添加圆角的方法
在OC中:
UIView *view2=[[UIView alloc]initWithFrame:CGRectMake(120,10,80,80)];
view2.backgroundColor=[UIColor redColor];[self.view addSubview:view2];
UIBezierPath*maskPath=[UIBezierPath bezierPathWithRoundedRect:view2.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(10,10)];
CAShapeLayer*maskLayer=[[CAShapeLayer alloc]init];
maskLayer.frame=view2.bounds;
maskLayer.path=maskPath.CGPath;
view2.layer.mask=maskLayer;
UIRectCornerBottomLeft|UIRectCornerBottomRight 意思是在view2的左下角和右下角设置圆角
在Swift3.0以前:
var rect =CGRect(x:0, y:0, width:200, height:40)
var textfield = UITextField(frame: rect)
textfield.backgroundColor=UIColor.lightGrayColor()// 背景颜色
textfield.text="GAGA"
textfield.textAlignment= NSTextAlignment.Center
textfield.textColor=UIColor.darkGrayColor()
var maskPath = UIBezierPath(roundedRect: textfield.bounds, byRoundingCorners: UIRectCorner.TopLeft| UIRectCorner.TopRight,// 左上右上圆角
cornerRadii:CGSize(width:12, height:12))// 圆角半径
/* 边框 */
var borderLayer = CAShapeLayer()
borderLayer.frame= textfield.bounds
borderLayer.path= maskPath.CGPath
borderLayer.strokeColor=UIColor.darkGrayColor().CGColor// 边框颜色borderLayer.fillColor=UIColor.clearColor().CGColor
/* 遮罩 */
var maskLayer = CAShapeLayer()
maskLayer.frame= textfield.bounds
maskLayer.path= maskPath.CGPath
textfield.layer.mask= maskLayer
textfield.layer.addSublayer(borderLayer)
可以看出UIRectCorner.TopLeft| UIRectCorner.TopRight和OC写法还是一样的,在textfield的左上和右上添加圆角
Swift3.0 :
let maskPath = UIBezierPath(roundedRect: (cell?.bounds)!, byRoundingCorners: [.topLeft, .topRight], cornerRadii:CGSize(width:10, height:10))
let masklayer =CAShapeLayer()
masklayer.frame= (cell?.bounds)!
masklayer.path= maskPath.cgPath
cell?.layer.mask= masklayer
这个方法里面[.topLeft, .topRight]也是表示与语法,在cell的左上和右上添加圆角,而且在Swift3.0
上面的写法都会报错,这应该也算去C写法,一个数组里面有两个选项添加在圆角类型上,而且这也更符合byRoundingCorners这个复数s。