This repository was archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathBrushExtensions.cs
199 lines (161 loc) · 5.09 KB
/
BrushExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System.Linq;
using CoreAnimation;
using CoreGraphics;
using UIKit;
namespace Xamarin.Forms.Platform.iOS
{
public static partial class BrushExtensions
{
const string BackgroundLayer = "BackgroundLayer";
public static void UpdateBackground(this UIView control, Brush brush)
{
if (control == null)
return;
UIView view = ShouldUseParentView(control) ? control.Superview : control;
// Remove previous background gradient layer if any
RemoveBackgroundLayer(view);
if (Brush.IsNullOrEmpty(brush))
return;
var backgroundLayer = GetBackgroundLayer(control, brush);
if (backgroundLayer != null)
{
control.BackgroundColor = UIColor.Clear;
view.InsertBackgroundLayer(backgroundLayer, 0);
}
}
public static CALayer GetBackgroundLayer(this UIView control, Brush brush)
{
if (control == null)
return null;
if (brush is SolidColorBrush solidColorBrush)
{
var linearGradientLayer = new CALayer
{
Name = BackgroundLayer,
ContentsGravity = CALayer.GravityResizeAspectFill,
Frame = control.Bounds,
BackgroundColor = solidColorBrush.Color.ToCGColor()
};
return linearGradientLayer;
}
if (brush is LinearGradientBrush linearGradientBrush)
{
var p1 = linearGradientBrush.StartPoint;
var p2 = linearGradientBrush.EndPoint;
var linearGradientLayer = new CAGradientLayer
{
Name = BackgroundLayer,
ContentsGravity = CALayer.GravityResizeAspectFill,
Frame = control.Bounds,
LayerType = CAGradientLayerType.Axial,
StartPoint = new CGPoint(p1.X, p1.Y),
EndPoint = new CGPoint(p2.X, p2.Y)
};
if (linearGradientBrush.GradientStops != null && linearGradientBrush.GradientStops.Count > 0)
{
var orderedStops = linearGradientBrush.GradientStops.OrderBy(x => x.Offset).ToList();
linearGradientLayer.Colors = GetCAGradientLayerColors(orderedStops);
linearGradientLayer.Locations = GetCAGradientLayerLocations(orderedStops);
}
return linearGradientLayer;
}
if (brush is RadialGradientBrush radialGradientBrush)
{
var center = radialGradientBrush.Center;
var radius = radialGradientBrush.Radius;
var radialGradientLayer = new CAGradientLayer
{
Name = BackgroundLayer,
ContentsGravity = CALayer.GravityResizeAspectFill,
Frame = control.Bounds,
LayerType = CAGradientLayerType.Radial,
StartPoint = new CGPoint(center.X, center.Y),
EndPoint = GetRadialGradientBrushEndPoint(center, radius),
CornerRadius = (float)radius
};
if (radialGradientBrush.GradientStops != null && radialGradientBrush.GradientStops.Count > 0)
{
var orderedStops = radialGradientBrush.GradientStops.OrderBy(x => x.Offset).ToList();
radialGradientLayer.Colors = GetCAGradientLayerColors(orderedStops);
radialGradientLayer.Locations = GetCAGradientLayerLocations(orderedStops);
}
return radialGradientLayer;
}
return null;
}
public static UIImage GetBackgroundImage(this UIView control, Brush brush)
{
if (control == null || brush == null || brush.IsEmpty)
return null;
var backgroundLayer = control.GetBackgroundLayer(brush);
if (backgroundLayer == null)
return null;
var renderer = new UIGraphicsImageRenderer(backgroundLayer.Bounds.Size, new UIGraphicsImageRendererFormat()
{
Opaque = false,
Scale = UIScreen.MainScreen.Scale,
});
return renderer.CreateImage((context) => backgroundLayer.RenderInContext(context.CGContext));
}
public static void InsertBackgroundLayer(this UIView view, CALayer backgroundLayer, int index = -1)
{
InsertBackgroundLayer(view.Layer, backgroundLayer, index);
}
public static void InsertBackgroundLayer(this CALayer layer, CALayer backgroundLayer, int index = -1)
{
RemoveBackgroundLayer(layer);
if (backgroundLayer != null)
{
if (index > -1)
layer.InsertSublayer(backgroundLayer, index);
else
layer.AddSublayer(backgroundLayer);
}
}
public static void RemoveBackgroundLayer(this UIView view)
{
if (view != null)
RemoveBackgroundLayer(view.Layer);
}
public static void RemoveBackgroundLayer(this CALayer layer)
{
if (layer != null)
{
if (layer.Name == BackgroundLayer)
layer?.RemoveFromSuperLayer();
if (layer.Sublayers == null || layer.Sublayers.Count() == 0)
return;
foreach (var subLayer in layer.Sublayers)
{
if (subLayer.Name == BackgroundLayer)
subLayer?.RemoveFromSuperLayer();
}
}
}
public static void UpdateBackgroundLayer(this UIView view)
{
if (view == null || view.Frame.IsEmpty)
return;
var layer = view.Layer;
UpdateBackgroundLayer(layer, view.Bounds);
}
static void UpdateBackgroundLayer(this CALayer layer, CGRect bounds)
{
if (layer != null && layer.Sublayers != null)
{
foreach (var sublayer in layer.Sublayers)
{
UpdateBackgroundLayer(sublayer, bounds);
if (sublayer.Name == BackgroundLayer && sublayer.Frame != bounds)
sublayer.Frame = bounds;
}
}
}
static bool ShouldUseParentView(UIView view)
{
if (view is UILabel)
return true;
return false;
}
}
}