Как создать градиент, который идет от полного цвета до непрозрачности 0 в iOS? - PullRequest
0 голосов
/ 14 июня 2019

Я посмотрел здесь , но, основываясь на их примере, который я попробовал и покажу ниже, не сработало. Не удалось выполнить следующее. Я ищу, чтобы создать градиент от полного черного до полного черного с непрозрачностью 0:

enter image description here

@IBDesignable
final class GradientView: UIView {
    @IBInspectable var startColor: UIColor = UIColor.clear
    @IBInspectable var endColor: UIColor = UIColor.clear

    override func draw(_ rect: CGRect) {
        let gradient: CAGradientLayer = CAGradientLayer()
//        gradient.frame = bounds
        gradient.frame = CGRect(x: CGFloat(0),
                                y: CGFloat(0),
                                width: superview!.frame.size.width,
                                height: superview!.frame.size.height)
        gradient.colors = [startColor.cgColor, endColor.cgColor]
        gradient.zPosition = -1
        layer.addSublayer(gradient)
    }
}

Как мне этого добиться, желательно в конструкторе интерфейсов?

Ответы [ 2 ]

1 голос
/ 15 июня 2019

В итоге я сделал следующее:

  private func setupGradient() { 

    //Below for container
    gradientViewContainer.frame = CGRect(x: 0, y: 152, width: 117, height: 38) 
    gradientViewContainer.isHidden = true

    //Below for actual gradient
    gradientLayer.frame = CGRect(x: 0, y: 152, width: gradientViewContainer.frame.width, height: gradientViewContainer.frame.height)

    gradientLayer.startPoint = CGPoint(x: 1.0, y: 0.0)
    gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.0)

    gradientLayer.colors = [UIColor.black.withAlphaComponent(0.0).cgColor, UIColor.black.withAlphaComponent(0.7).cgColor, UIColor.black.withAlphaComponent(0.7).cgColor,  UIColor.black.withAlphaComponent(1.0).cgColor]
    gradientLayer.locations = [0.0, 1.0]

    gradientViewContainer.layer.insertSublayer(gradientLayer, at: 0)
    gradientViewContainer.alpha = 0.65

}

Затем в viewDidLoad:

    setupGradient()
    myView.layer.addSublayer(gradientLayer)
1 голос
/ 15 июня 2019

Следующая моя реализация

@IBDesignable
final class GradientView: UIView {
    override func draw(_ rect: CGRect) { 
        //// General Declarations
        let context = UIGraphicsGetCurrentContext()!


        //// Gradient Declarations
        let gradient = CGGradient(colorsSpace: nil, colors: [UIColor.white.cgColor, 
     UIColor.white.blended(withFraction: 0.5, of: UIColor.black).cgColor, 
    UIColor.black.cgColor] as CFArray, locations: [0, 0.51, 0.89])!

    //// Rectangle Drawing
    let rectangleRect = CGRect(x: frame.minX + 11, y: frame.minY + 8, width: 85, height: 54)
    let rectanglePath = UIBezierPath(rect: rectangleRect)
    context.saveGState()
    rectanglePath.addClip()
    context.drawLinearGradient(gradient,
        start: CGPoint(x: rectangleRect.midX, y: rectangleRect.minY),
        end: CGPoint(x: rectangleRect.midX, y: rectangleRect.maxY),
        options: [])
    context.restoreGState()
    }
}
...