iOS渐变不垂直于渐变向量

时间:2017-10-18 11:06:24

标签: ios swift core-animation gradient layer

我在组件上添加了一个渐变,它是一个角度。该梯度不垂直于梯度向量。为方便起见,设备iPhone 6s已将大小设置为常量。可能是什么问题?

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    let topPoint = CGPoint(x: 33, y: -55)
    let botPoint = CGPoint(x: 217, y: 469)

    let iPhoneSize = CGSize(width: 375, height: 667)

    let additionalLayer = CAGradientLayer()
    additionalLayer.frame = view.bounds

    additionalLayer.startPoint = CGPoint(x: topPoint.x / iPhoneSize.width, y: topPoint.y / iPhoneSize.height)
    additionalLayer.endPoint = CGPoint(x: botPoint.x / iPhoneSize.width, y: botPoint.y / iPhoneSize.height)
    additionalLayer.colors = [UIColor.white.cgColor, UIColor.darkGray.cgColor, UIColor.black.cgColor]
    additionalLayer.locations = [0.0, 0.468, 1.0]
    drawLine(onLayer: additionalLayer, fromPoint: topPoint, toPoint: botPoint)
    view.layer.addSublayer(additionalLayer)
}

func drawLine(onLayer layer: CALayer, fromPoint start: CGPoint, toPoint end: CGPoint) {
    let line = CAShapeLayer()
    let linePath = UIBezierPath()
    linePath.move(to: start)
    linePath.addLine(to: end)
    line.path = linePath.cgPath
    line.fillColor = nil
    line.opacity = 1.0
    line.strokeColor = UIColor.red.cgColor
    layer.addSublayer(line)
}

}

P.S。我已经尝试将此代码添加到viewDidLayoutSubviews()

P.P.S。还添加了截图。

enter image description here

0 个答案:

没有答案