|
| 1 | +using System; |
| 2 | +using System.ComponentModel; |
| 3 | +using System.Linq; |
| 4 | +using AiForms.Effects; |
| 5 | +using AiForms.Effects.iOS; |
| 6 | +using CoreAnimation; |
| 7 | +using CoreGraphics; |
| 8 | +using UIKit; |
| 9 | +using Xamarin.Forms; |
| 10 | +using Xamarin.Forms.Platform.iOS; |
| 11 | + |
| 12 | +[assembly: ExportEffect(typeof(GradientPlatformEffect), nameof(Gradient))] |
| 13 | +namespace AiForms.Effects.iOS |
| 14 | +{ |
| 15 | + public class GradientPlatformEffect:PlatformEffect |
| 16 | + { |
| 17 | + UIView _view; |
| 18 | + VisualElement _visualElement; |
| 19 | + CAGradientLayer _layer; |
| 20 | + bool _clipsToBounds; |
| 21 | + |
| 22 | + protected override void OnAttached() |
| 23 | + { |
| 24 | + _view = Control ?? Container; |
| 25 | + _visualElement = Element as VisualElement; |
| 26 | + if(_visualElement == null) |
| 27 | + { |
| 28 | + Device.BeginInvokeOnMainThread(() => Gradient.SetOn(Element, false)); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + if (Element is Label) |
| 33 | + { |
| 34 | + _view = Container; |
| 35 | + } |
| 36 | + |
| 37 | + _clipsToBounds = _view.ClipsToBounds; |
| 38 | + _visualElement.SizeChanged += OnSizeChanged; |
| 39 | + UpdateGradient(); |
| 40 | + } |
| 41 | + |
| 42 | + protected override void OnDetached() |
| 43 | + { |
| 44 | + if(_visualElement != null) |
| 45 | + { |
| 46 | + _visualElement.SizeChanged -= OnSizeChanged; |
| 47 | + } |
| 48 | + |
| 49 | + if(_view != null) |
| 50 | + { |
| 51 | + _view.ClipsToBounds = _clipsToBounds; |
| 52 | + } |
| 53 | + |
| 54 | + _layer?.RemoveFromSuperLayer(); |
| 55 | + _layer?.Dispose(); |
| 56 | + _layer = null; |
| 57 | + |
| 58 | + _view = null; |
| 59 | + _visualElement = null; |
| 60 | + |
| 61 | + System.Diagnostics.Debug.WriteLine($"Detached {GetType().Name} from {Element.GetType().FullName}"); |
| 62 | + } |
| 63 | + |
| 64 | + protected override void OnElementPropertyChanged(PropertyChangedEventArgs args) |
| 65 | + { |
| 66 | + base.OnElementPropertyChanged(args); |
| 67 | + if(args.PropertyName == Gradient.ColorsProperty.PropertyName || |
| 68 | + args.PropertyName == Gradient.OrientationProperty.PropertyName) |
| 69 | + { |
| 70 | + UpdateGradient(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private void OnSizeChanged(object sender, EventArgs e) |
| 75 | + { |
| 76 | + UpdateGradient(); |
| 77 | + } |
| 78 | + |
| 79 | + void UpdateGradient() |
| 80 | + { |
| 81 | + var colors = Gradient.GetColors(Element); |
| 82 | + if(colors == null) |
| 83 | + { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + _layer?.RemoveFromSuperLayer(); |
| 88 | + _layer?.Dispose(); |
| 89 | + |
| 90 | + _layer = new CAGradientLayer(); |
| 91 | + _layer.Frame = new CGRect(0, 0, _visualElement.Width, _visualElement.Height); |
| 92 | + _layer.Colors = colors.Select(x => x.ToCGColor()).ToArray(); |
| 93 | + (_layer.StartPoint,_layer.EndPoint) = ConvertPoint(); |
| 94 | + _view.Layer.InsertSublayer(_layer, 0); |
| 95 | + _view.ClipsToBounds = true; |
| 96 | + } |
| 97 | + |
| 98 | + (CGPoint start,CGPoint end) ConvertPoint() |
| 99 | + { |
| 100 | + var orientation = Gradient.GetOrientation(Element); |
| 101 | + |
| 102 | + switch(orientation) |
| 103 | + { |
| 104 | + case GradientOrientation.LeftRight: |
| 105 | + return (new CGPoint(0, 0.5), new CGPoint(1, 0.5)); |
| 106 | + case GradientOrientation.BlTr: |
| 107 | + return (new CGPoint(0, 1.0), new CGPoint(1, 0)); |
| 108 | + case GradientOrientation.BottomTop: |
| 109 | + return (new CGPoint(0.5, 1), new CGPoint(0.5, 0)); |
| 110 | + case GradientOrientation.BrTl: |
| 111 | + return (new CGPoint(1, 1), new CGPoint(0, 0)); |
| 112 | + case GradientOrientation.RightLeft: |
| 113 | + return (new CGPoint(1, 0.5), new CGPoint(0, 0.5)); |
| 114 | + case GradientOrientation.TrBl: |
| 115 | + return (new CGPoint(1, 0), new CGPoint(0, 1)); |
| 116 | + case GradientOrientation.TopBottom: |
| 117 | + return (new CGPoint(0.5, 0), new CGPoint(0.5, 1)); |
| 118 | + case GradientOrientation.TlBr: |
| 119 | + return (new CGPoint(0, 0), new CGPoint(1, 1)); |
| 120 | + default: |
| 121 | + return (new CGPoint(0, 0.5), new CGPoint(1, 0.5)); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments