SwiftUI · iOS 18+
@available(iOS 18.0, *) func makeSwiftUIMesh( palette: [UIColor] ) -> some View { let points: [SIMD2<Float>] = [ [0, 0], [1, 0], [0, 1], [1, 1] ] let colors: [Color] = palette.map({ (color: UIColor) -> Color in Color(uiColor: color) }) return MeshGradient( width: 2, height: 2, points: points, colors: colors, colorSpace: Gradient.ColorSpace.device ) .frame(width: 360, height: 240) }
UIKit + Metal · iOS 15+
func makeUIKitMesh( palette: [UIColor] ) -> KHMeshGradientView { let gradient: KHMeshGradientView = KHMeshGradientView() gradient.meshSize = KHMeshGradientView.MeshSize( width: 2, height: 2 ) gradient.points = [ CGPoint(x: 0, y: 0), CGPoint(x: 1, y: 0), CGPoint(x: 0, y: 1), CGPoint(x: 1, y: 1) ] gradient.colors = palette gradient.colorSpace = KHMeshGradientView.ColorSpace.device gradient.frame = CGRect( x: 0, y: 0, width: 360, height: 240 ) return gradient }