如何为蒙版的UIView swift设置边框颜色

时间:2017-06-27 01:30:28

标签: swift uiview

我使用了从SO获得的以下功能来创建菱形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)

有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您已使用CAShapeLayer(),因此您无需添加带边框的视图。只定制CAShapeLayer()就足够了。

shapeLayer.lineWidth = 2.0 

尝试使用CAShapeLayer()