Applying edge fill to shape

off-topic

(Ben) #1

I have been trying unsuccessfully to fill a shape with a gradient. Using CAGradientLayer and CAShapeLayer on the UIBezierPath, but it seems to be for rectangles, not polygons.

The matter is further complicated as I am looking to fill from the edge of the shape and into its central point (startPoint and endPoint?), not from one edge to the other.

import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let triangle = Triangle(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height))
        triangle.backgroundColor = UIColor.red
        view.addSubview(triangle)
        
    }
}


class Triangle: UIView {
    
    override func draw(_ rect: CGRect) {        
        
        func fromLocation(angle: CGFloat, radius: CGFloat, offset: CGPoint) -> CGPoint {
            return CGPoint(x: radius * cos(angle) + offset.x, y: radius * sin(angle) + offset.y)
            
        }
        

        
        func drawTriangle() -> () {
            
                let degreeChange = arc4random() % 10 + 3
                let path = UIBezierPath()
                
                
                var angleChange: CGFloat = -CGFloat(Double.pi / 2.0)
                let angleIncrement = CGFloat(Double.pi * 2.0 / 3)
                let radius = rect.width / 2.0
                
                var center = CGPoint(x: rect.width / 2, y: rect.height / 2)
                


                let triangleGradient = CAGradientLayer()
                triangleGradient.frame = path.bounds
                
                
                let cgGreenTrans = UIColor(red: 0.0/255.0, green: 255.0/255.0, blue: 0.0/255.0, alpha: 0.2).cgColor
                
                triangleGradient.colors = [UIColor.red.cgColor, cgGreenTrans]
            
                
                //gradient fill
                let shapeMask = CAShapeLayer()
                shapeMask.path = path.cgPath
                
                triangleGradient.mask = shapeMask
                layer.addSublayer(shapeMask)
                
                layer.addSublayer(triangleGradient)
                
                
                path.move(to: fromLocation(angle: angleChange, radius: radius, offset: center))
                
                
                
                for _ in 1...degreeChange - 1 {
                    
                    angleChange += angleIncrement
                    path.addLine(to: fromLocation(angle: angleChange, radius: radius, offset: center))
                    
                    //print out elements ?
                    print(path.accessibilityElementCount())
                }
                
                path.close()
                path.fill()
                center = CGPoint(x: 0.0, y: 0.0)
        }
        drawTriangle()
        
    }
    
}

I also notice that no elements are printed into the lldb?


(Quinn “The Eskimo!”) #2

As this question is about various graphics APIs on iOS, rather than the Swift language itself, you might have more luck asking it in the Cocoa Touch topic area of DevForums.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple