[英]UIBezierPath and CAShapeLayer not creating corner line
這是我的代碼
public func setMaskTo(setMask:UIView, corner:UIRectCorner) -> Void {
let round = UIBezierPath(roundedRect: setMask.bounds, byRoundingCorners: corner, cornerRadii: CGSize(width: 10, height: 10))
let shape = CAShapeLayer()
shape.frame = round.bounds
shape.path = round.cgPath
setMask.layer.mask = shape
}
setMaskTo(setMask: self, corner: [.topLeft, .topRight])
這是我的輸出
誰能解釋我錯在哪里? 在Swift 3上。
謝謝,
嘿,我得到了答案!!!
嘗試使用UIBezierPath和CAShapeLayer不創建角線
var borderShape = CAShapeLayer()
borderShape.path = round.cgPath
borderShape.lineWidth = 1.0
borderShape.strokeColor = UIColor.blue.cgColor
borderShape.fillColor = nil
borderShape.frame = setMask.bounds
setMask.layer.addSublayer(borderShape)
更好地使用通用擴展名:
extension UIView {
func roundedCorners(corners: UIRectCorner, conrnerRadius: CGFloat) {
let rectShape = CAShapeLayer()
rectShape.bounds = self.frame
rectShape.position = self.center
rectShape.path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: [UIRectCorner.TopLeft , UIRectCorner.TopRight], cornerRadii: CGSizeMake(conrnerRadius, conrnerRadius)).CGPath
self.layer.mask = rectShape
}
}
調用函數:
oneextraview.roundedCorners(corners: [UIRectCorner.topRight, UIRectCorner.bottomRight], conrnerRadius: 10.0)
oneextraview.layer.borderColor = UIColor.redColor().CGColor
oneextraview.layer.borderWidth = 5.0
oneextraview.layer.masksToBounds = true
注意:在類外部的ViewController文件中或在單獨的swift文件中聲明擴展
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.