4

I have added a gradient on the component, it is at an angle. This gradient is not perpendicular to the gradient vector. Device iPhone 6s, for convenience, have set the size constant. What could be the problem?

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. I've tried add this code to viewDidLayoutSubviews()

P.P.S. Also have added screenshot.

enter image description here

  • Whoops... my answer was incorrect. Here is (I believe) what you are looking for: https://stackoverflow.com/a/43176174/6257435 – DonMag Oct 18 '17 at 13:07

0 Answers0