【发布时间】:2018-12-10 11:25:57
【问题描述】:
我有一个 UIView,其中包含一个 2 UILabels,里面有一个按钮,我想在它的边框上添加一个渐变色。我设法添加了它,并且按钮最终移到了自定义 UIView 之外,而自定义 UIView 在较小的设备上也一直向外缩小。添加gradient color时如何修复UIView以保持相同大小
这里是最初的UIView,前面有两个UILabels和一个带有正常边框颜色的按钮
这里是应用渐变色后的样子
这是我如何应用渐变的代码。
@IBOutlet weak var customView: UIView!
let gradient = CAGradientLayer()
gradient.frame.size = self.customView.frame.size
gradient.colors = [UIColor.green.cgColor, UIColor.yellow.cgColor, UIColor.red.cgColor]
gradient.startPoint = CGPoint(x: 0.1, y: 0.78)
gradient.endPoint = CGPoint(x: 1.0, y: 0.78)
let shapeLayer = CAShapeLayer()
shapeLayer.path = UIBezierPath(rect: self.customView.bounds).cgPath
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = UIColor.black.cgColor
shapeLayer.lineWidth = 4
gradient.mask = shapeLayer
self.customView.layer.addSublayer(gradient)
【问题讨论】:
-
也显示自动布局约束...
-
限制条件很多。不知道如何在这里列出所有这些?
-
for startpoint 和 Endpoint 你可以使用视图中的值而不是使用固定值
-
@Ruban4Axis 以及我是如何做到的。两个点都包含 X 和 Y 位置,包括起点和终点?
标签: ios swift gradient calayer cagradientlayer