使用UIBezierPath给view切圆角,为什么只对一边有效
展开全部
override func drawRect(rect: CGRect) {
//切左上右上圆角
let maskPath = UIBezierPath(roundedRect:self.bounds, byRoundingCorners: [.TopLeft,.TopRight], cornerRadii: CGSizeMake(5.0, 5.0))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.bounds
maskLayer.path = maskPath.CGPath
self.layer.mask = maskLayer
//添加border
let borderLayer = CAShapeLayer()
borderLayer.frame = self.bounds
borderLayer.path = maskPath.CGPath
borderLayer.lineWidth = 0.5
borderLayer.strokeColor = UIColor.lightGrayColor().CGColor
borderLayer.fillColor = UIColor.clearColor().CGColor
let layers:NSArray = self.layer.sublayers! as NSArray
if ((layers.lastObject!.isKindOfClass(CAShapeLayer))) {
layers.lastObject?.removeFromSuperlayer()
}
self.layer.addSublayer(borderLayer)
}
//切左上右上圆角
let maskPath = UIBezierPath(roundedRect:self.bounds, byRoundingCorners: [.TopLeft,.TopRight], cornerRadii: CGSizeMake(5.0, 5.0))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.bounds
maskLayer.path = maskPath.CGPath
self.layer.mask = maskLayer
//添加border
let borderLayer = CAShapeLayer()
borderLayer.frame = self.bounds
borderLayer.path = maskPath.CGPath
borderLayer.lineWidth = 0.5
borderLayer.strokeColor = UIColor.lightGrayColor().CGColor
borderLayer.fillColor = UIColor.clearColor().CGColor
let layers:NSArray = self.layer.sublayers! as NSArray
if ((layers.lastObject!.isKindOfClass(CAShapeLayer))) {
layers.lastObject?.removeFromSuperlayer()
}
self.layer.addSublayer(borderLayer)
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询