【发布时间】:2015-10-23 10:22:14
【问题描述】:
我画了一条曲线。我想从视图一直到直线放置一个渐变(以便渐变曲线与直线一起。)
编辑:这是下面代码产生的照片:
我知道如何画线,也知道如何添加常规渐变,但不能将两者结合在一起。这是我的代码:
override func drawRect(rect: CGRect) {
// draw the curved line, this code works just fine.
let path1 = UIBezierPath()
path1.lineWidth = 1.1
UIColor.greenColor().setStroke()
path1.moveToPoint(CGPoint(x: 0, y: bounds.height/2))
path1.addQuadCurveToPoint(CGPoint(x: bounds.width, y: bounds.height/2), controlPoint: CGPoint(x: bounds.width/2, y: (bounds.height * 0.75)))
path1.stroke()
// my attempt to draw the gradient:
let gradient = CAGradientLayer()
gradient.startPoint = CGPoint(x: 1, y: 1)
gradient.endPoint = CGPoint(x: 0, y: 0)
let colors = [UIColor.whiteColor().CGColor, UIColor(red: 0, green: 1, blue: 1, alpha: 0.4).CGColor]
gradient.colors = colors
// the following line is where I need help
gradient.frame = CGRectMake(0, 475, bounds.width, path1.bounds.height)
layer.addSublayer(gradient)
}
我可以设置什么 gradient.frame 等于它的上限是以前绘制的路径?请用 Swift 回答(我已经看到了很多关于这个主题的其他问题,但它们都在目标 C 中)
谢谢
【问题讨论】:
标签: ios swift core-graphics uibezierpath