姬長信(Redy)

关于UIImageView自定义的建议

我试图找出在代码中重新创建此图像的最佳方法。 我已经考虑过两个UIImageViews并通过约束连接它们/uff0c但这只能让我获得50/uff05的路径/uff0c因为没有对角白线分割这两种独特的颜色。 我还希望能够以编程方式更改UIImageView的每一半的颜色。

 

下面主要区别在于我实现了@IBDesignable和@IBInspectable/uff0c因此您可以在Storyboard / IB中看到它并进行调整 

        let x1 = bounds.minX
        let y1 = bounds.minY
        let x2 = bounds.maxX
        let y2 = bounds.maxY

        var path = UIBezierPath()

        let offset = (bounds.width / 2) * tan(divAngle * CGFloat.pi / 180)

        path.move(to: CGPoint(x: x1, y: y1))
        path.addLine(to: CGPoint(x: x2 / 2.0 - divWidth / 2.0 + offset, y: y1))
        path.addLine(to: CGPoint(x: x2 / 2.0 - divWidth / 2.0 - offset, y: y2))
        path.addLine(to: CGPoint(x: x1, y: y2))
        path.close()

        leftLayer.path = path.cgPath

        path = UIBezierPath()

        path.move(to: CGPoint(x: x2 / 2.0 + divWidth / 2.0 + offset, y: y1))
        path.addLine(to: CGPoint(x: x2, y: y1))
        path.addLine(to: CGPoint(x: x2, y: y2))
        path.addLine(to: CGPoint(x: x2 / 2.0 + divWidth / 2.0 - offset, y: y2))
        path.close()

        rightLayer.path = path.cgPath

        leftLayer.fillColor = leftColor.cgColor
        rightLayer.fillColor = rightColor.cgColor

        maskLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: radius).cgPath
        layer.mask = maskLayer

        backgroundColor = divColor

    }
}

使用默认值/uff1a

结果/uff1a

和一些变化/uff1a

结果/uff1a

退出移动版