源码

关于UIImageView自定义的建议

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

Image I'm trying to recreate 

下面主要区别在于我实现了@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

enter image description here

结果/uff1a

enter image description here

和一些变化/uff1a

enter image description here

结果/uff1a

enter image description here

(0)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/guanyuuiimageviewzidingyidejianyi.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:8 月 25, 2019 at 04:22 下午

热评文章