如何为蒙版的uiview Swift设置边框颜色



我使用了以下功能,从而创建了钻石形状的uiviews:

func addDiamondMaskToView(view: UIView) {
        let path = UIBezierPath()
        path.moveToPoint(CGPoint(x: view.bounds.size.width / 2.0, y: 0))
        path.addLineToPoint(CGPoint(x: view.bounds.size.width, y: view.bounds.size.height / 2.0))
        path.addLineToPoint(CGPoint(x: view.bounds.size.width / 2.0, y: view.bounds.size.height))
        path.addLineToPoint(CGPoint(x: 0, y: view.bounds.size.height / 2.0))
        path.closePath()
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = path.CGPath
        shapeLayer.fillColor = UIColor.whiteColor().CGColor
        shapeLayer.strokeColor = UIColor.clearColor().CGColor
        view.layer.mask = shapeLayer
    }

但是,当我使用以下代码在其周围创建边框时,您只能看到钻石的边缘,因为出于某种原因,旧的正方形形状正在阻止它

            testView.layer.borderWidth = 2.0
            testView.layer.borderColor = UIColor.blueColor().CGColor
            testView2.layer.borderWidth = 2.0
            testView2.layer.borderColor = UIColor.blueColor().CGColor
            testView3.layer.borderWidth = 2.0
            testView3.layer.borderColor = UIColor.blueColor().CGColor
            addDiamondMaskToView(testView)
            addDiamondMaskToView(testView2)
            addDiamondMaskToView(testView3)

有人知道我如何解决此问题吗?

您已经使用了CashApelayer((,因此您无需使用边框添加视图。只有自定义CashApelayer((就足够了。

shapeLayer.lineWidth = 2.0 

与您的CashApelayer((

一起尝试

最新更新