我的CALayer配置如下:
private func setup() {
guard let theLayer = self.layer as? CAGradientLayer else {
return;
}
theLayer.colors = [UIColor.red.cgColor, UIColor.blue.cgColor]
theLayer.locations = [0.0, 1.0]
theLayer.startPoint = CGPoint(x: 0, y: 0)
theLayer.endPoint = CGPoint(x: 1, y: 0)
theLayer.frame = self.bounds
}
我想根据用户输入值(在下面的代码中称为“值”)将其裁剪为一个矩形。我该怎么办?
override func draw(_ rect: CGRect) {
// Drawing code
let frame = CGRect(x: 0, y: 0, width: rect.width, height: rect.height * (1-value))
// How to clip or mask self.layer to above frame?
}
答案 0 :(得分:0)
将该层嵌入具有适当frame
的容器层中,并将其masksToBounds
属性设置为true
。