Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Radial gradient #508

Closed
tbaranes opened this issue Sep 8, 2017 · 2 comments
Closed

Radial gradient #508

tbaranes opened this issue Sep 8, 2017 · 2 comments

Comments

@tbaranes
Copy link
Member

tbaranes commented Sep 8, 2017

Would be great to support both gradient type: Linear AND Radial

@JakeLin
Copy link
Member

JakeLin commented Sep 9, 2017

Good idea. CAGradientLayer on iOS doesn't support radial gradient, we need to draw it using CGContextDrawRadialGradient by ourselves.

@phimage
Copy link
Member

phimage commented Oct 22, 2017

screen shot 2017-10-22 at 22 19 49

Just some test by using a new CALayer class instead of CAGradientLayer
Maybe an enum like GradientMode with linear or radial must created to select the good layer

class RadialGradientLayer: CALayer {
  
  var startPoint:CGPoint = .zero
  var endPoint:CGPoint = .zero
  var colors: [CGColor] = [UIColor.clear.cgColor, UIColor.clear.cgColor]
  override init(){
    super.init()
    needsDisplayOnBoundsChange = true
  }
  required init(coder aDecoder: NSCoder) {
    super.init()
  }
  
  override func draw(in ctx: CGContext) {
    ctx.saveGState()
    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let gradient = CGGradient(colorsSpace: colorSpace, colors: colors as CFArray, locations: [0.0,1.0])!
    let size = self.frame.size
    let radius = max(max(size.width - endPoint.x, endPoint.x), max(size.height - endPoint.y, endPoint.y))
    ctx.drawRadialGradient(gradient, startCenter: startPoint, startRadius: 0, endCenter: endPoint, endRadius: radius, options: CGGradientDrawingOptions.drawsAfterEndLocation)
  }

all the startPoint, endPoint etc... must be scale to the view frame.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants