MCPcopy Create free account
hub / github.com/NativeScript/NativeScript / drawGradient

Function drawGradient

packages/core/ui/utils.ios.ts:37–78  ·  view source on GitHub ↗
(nativeView: UIView, gradientLayer: CAGradientLayer, gradient: LinearGradient, gradientLayerOpacity?: number)

Source from the content-addressed store, hash-verified

35 }
36
37 export function drawGradient(nativeView: UIView, gradientLayer: CAGradientLayer, gradient: LinearGradient, gradientLayerOpacity?: number): void {
38 if (!nativeView || !gradient) {
39 return;
40 }
41
42 if (typeof gradientLayerOpacity === 'number') {
43 gradientLayer.opacity = gradientLayerOpacity;
44 }
45
46 // Update these properties instead of layer frame as the latter messes with animations
47 gradientLayer.bounds = nativeView.bounds;
48 gradientLayer.anchorPoint = CGPointMake(0, 0);
49
50 gradientLayer.allowsEdgeAntialiasing = true;
51 gradientLayer.contentsScale = Screen.mainScreen.scale;
52
53 const iosColors = NSMutableArray.alloc().initWithCapacity(gradient.colorStops.length);
54 const iosStops = NSMutableArray.alloc<number>().initWithCapacity(gradient.colorStops.length);
55 let hasStops = false;
56
57 gradient.colorStops.forEach((stop) => {
58 iosColors.addObject(stop.color.ios.CGColor);
59 if (stop.offset) {
60 iosStops.addObject(stop.offset.value);
61 hasStops = true;
62 }
63 });
64
65 gradientLayer.colors = iosColors;
66
67 if (hasStops) {
68 gradientLayer.locations = iosStops;
69 }
70
71 const alpha = gradient.angle / (Math.PI * 2);
72 const startX = Math.pow(Math.sin(Math.PI * (alpha + 0.75)), 2);
73 const startY = Math.pow(Math.sin(Math.PI * (alpha + 0.5)), 2);
74 const endX = Math.pow(Math.sin(Math.PI * (alpha + 0.25)), 2);
75 const endY = Math.pow(Math.sin(Math.PI * alpha), 2);
76 gradientLayer.startPoint = { x: startX, y: startY };
77 gradientLayer.endPoint = { x: endX, y: endY };
78 }
79}

Callers

nothing calls this directly

Calls 2

forEachMethod · 0.80
addObjectMethod · 0.80

Tested by

no test coverage detected