Example #1
0
        private CALayer CreateLayer(CGPath path)
        {
            var pathLayer = new CAShapeLayer()
            {
                Path        = path,
                StrokeColor = (Stroke as SolidColorBrush)?.ColorWithOpacity ?? Colors.Transparent,
                LineWidth   = (nfloat)ActualStrokeThickness,
            };

            switch (Fill)
            {
            case SolidColorBrush colorFill:
                pathLayer.FillColor = colorFill.ColorWithOpacity;
                break;

            case ImageBrush imageFill when TryCreateImageBrushLayers(imageFill, GetFillMask(path), out var imageLayer):
                pathLayer.FillColor = Colors.Transparent;

                pathLayer.AddSublayer(imageLayer);
                break;

            case LinearGradientBrush gradientFill:
                var gradientLayer = gradientFill.GetLayer(Frame.Size);
                gradientLayer.Frame         = Bounds;
                gradientLayer.Mask          = GetFillMask(path);
                gradientLayer.MasksToBounds = true;

                pathLayer.FillColor = Colors.Transparent;
                pathLayer.AddSublayer(gradientLayer);
                break;

            case null:
                pathLayer.FillColor = Colors.Transparent;
                break;

            default:
                Application.Current.RaiseRecoverableUnhandledException(new NotSupportedException($"The brush {Fill} is not supported as Fill for a {this} on this platform."));
                pathLayer.FillColor = Colors.Transparent;
                break;
            }

            if (StrokeDashArray != null)
            {
                var pattern = StrokeDashArray.Select(d => (global::Foundation.NSNumber)d).ToArray();

                pathLayer.LineDashPhase   = 0;               // Starting position of the pattern
                pathLayer.LineDashPattern = pattern;
            }

            return(pathLayer);

            CAShapeLayer GetFillMask(CGPath mask)
            => new CAShapeLayer
            {
                Path  = mask,
                Frame = Bounds,
                // We only use the fill color to create the mask area
                FillColor = _Color.White.CGColor,
            };
        }
Example #2
0
 CALayer SetupLayers()
 {
     _colorAnimationRing = SetupBackgroundLayer();
     _colorAnimationRing.AddSublayer(SetupProgressLayer());
     _colorAnimationRing.AddSublayer(SetupTextLayer());
     _colorAnimationRing.Contents = ProgressButtonImages.BackgroundImage.AsResourceNsImage().CGImage;
     return(_colorAnimationRing);
 }
Example #3
0
        void DrawLandmark(VNFaceLandmarkRegion2D feature, CGRect scaledBoundingBox, bool closed, UIColor color)
        {
            if (feature == null)
            {
                return;
            }

            var mappedPoints = feature.NormalizedPoints.Select(o => new CGPoint(x: o.X * scaledBoundingBox.Width + scaledBoundingBox.X, y: o.Y * scaledBoundingBox.Height + scaledBoundingBox.Y));

            using (var newLayer = new CAShapeLayer())
            {
                newLayer.Frame       = _view.Frame;
                newLayer.StrokeColor = color.CGColor;
                newLayer.LineWidth   = 2;
                newLayer.FillColor   = UIColor.Clear.CGColor;

                using (UIBezierPath path = new UIBezierPath())
                {
                    path.MoveTo(mappedPoints.First());
                    foreach (var point in mappedPoints.Skip(1))
                    {
                        path.AddLineTo(point);
                    }

                    if (closed)
                    {
                        path.AddLineTo(mappedPoints.First());
                    }

                    newLayer.Path = path.CGPath;
                }

                _shapeLayer.AddSublayer(newLayer);
            }
        }
Example #4
0
        private void DrawMarker(RectangleF rowRect, float position)
        {
            if (Layer.Sublayers != null)
            {
                Layer.Sublayers = new CALayer[0];
            }

            var visibleRect     = Layer.Bounds;
            var currentTimeRect = visibleRect;

            // The red band of the timeMaker will be 7 pixels wide
            currentTimeRect.X     = 0f;
            currentTimeRect.Width = 7f;

            var timeMarkerRedBandLayer = new CAShapeLayer();

            timeMarkerRedBandLayer.Frame    = currentTimeRect;
            timeMarkerRedBandLayer.Position = new PointF(rowRect.X, Bounds.Height / 2f);

            var linePath = CGPath.FromRect(currentTimeRect);

            timeMarkerRedBandLayer.FillColor = UIColor.FromRGBA(1.00f, 0.00f, 0.00f, 0.50f).CGColor;

            timeMarkerRedBandLayer.Path = linePath;

            currentTimeRect.X     = 0f;
            currentTimeRect.Width = 1f;

            CAShapeLayer timeMarkerWhiteLineLayer = new CAShapeLayer();

            timeMarkerWhiteLineLayer.Frame    = currentTimeRect;
            timeMarkerWhiteLineLayer.Position = new PointF(3f, Bounds.Height / 2f);

            CGPath whiteLinePath = CGPath.FromRect(currentTimeRect);

            timeMarkerWhiteLineLayer.FillColor = UIColor.FromRGBA(1.00f, 1.00f, 1.00f, 1.00f).CGColor;
            timeMarkerWhiteLineLayer.Path      = whiteLinePath;

            timeMarkerRedBandLayer.AddSublayer(timeMarkerWhiteLineLayer);
            CABasicAnimation scrubbingAnimation = new CABasicAnimation();

            scrubbingAnimation.KeyPath = "position.x";

            scrubbingAnimation.From = new NSNumber(HorizontalPositionForTime(CMTime.Zero));
            scrubbingAnimation.To   = new NSNumber(HorizontalPositionForTime(duration));
            scrubbingAnimation.RemovedOnCompletion = false;
            scrubbingAnimation.BeginTime           = 0.000000001;
            scrubbingAnimation.Duration            = duration.Seconds;
            scrubbingAnimation.FillMode            = CAFillMode.Both;
            timeMarkerRedBandLayer.AddAnimation(scrubbingAnimation, null);

            Console.WriteLine("Duration in  seconds - " + Player.CurrentItem.Asset.Duration.Seconds);
            var syncLayer = new AVSynchronizedLayer()
            {
                PlayerItem = Player.CurrentItem,
            };

            syncLayer.AddSublayer(timeMarkerRedBandLayer);
            Layer.AddSublayer(syncLayer);
        }
Example #5
0
        private void SetFillAndStroke(CAShapeLayer pathLayer)
        {
            RemoveSublayers();

            switch (Fill)
            {
            case SolidColorBrush colorFill:
                pathLayer.FillColor = colorFill.ColorWithOpacity;
                break;

            case ImageBrush imageFill when TryCreateImageBrushLayers(imageFill, GetFillMask(pathLayer.Path), out var imageLayer):
                pathLayer.FillColor = Colors.Transparent;

                pathLayer.AddSublayer(imageLayer);
                break;

            case GradientBrush gradientFill:
                var gradientLayer = gradientFill.GetLayer(Frame.Size);
                gradientLayer.Frame = Bounds;
                gradientLayer.Mask ??= GetFillMask(pathLayer.Path);
                gradientLayer.MasksToBounds = true;

                pathLayer.FillColor = Colors.Transparent;
                pathLayer.AddSublayer(gradientLayer);
                break;

            case null:
                pathLayer.FillColor = Colors.Transparent;
                break;

            default:
                Application.Current.RaiseRecoverableUnhandledException(new NotSupportedException($"The brush {Fill} is not supported as Fill for a {this} on this platform."));
                pathLayer.FillColor = Colors.Transparent;
                break;
            }

            pathLayer.StrokeColor = Brush.GetColorWithOpacity(Stroke, Colors.Transparent);
            pathLayer.LineWidth   = (nfloat)ActualStrokeThickness;

            if (StrokeDashArray is { } sda)
            {
                var pattern = sda.Select(d => (global::Foundation.NSNumber)d).ToArray();

                pathLayer.LineDashPhase   = 0;               // Starting position of the pattern
                pathLayer.LineDashPattern = pattern;
            }
Example #6
0
 CALayer SetupLayers()
 {
     _progressLayer = SetupProgressLayer();
     if (_isShowsText == 1)
     {
         _progressLayer.AddSublayer(SetupTextLayer());
     }
     _backgroundLayer = SetupBackgroundLayer();
     _backgroundLayer.AddSublayer(_progressLayer);
     return(_backgroundLayer);
 }
Example #7
0
        public static CAShapeLayer ToShape(this GPath element)
        {
            var shape = new CAShapeLayer();

            foreach (var item in element.Paths)
            {
                if (item is GPath gp)
                {
                    shape.AddSublayer(gp.ToShape());
                }
                else if (item is Path pa)
                {
                    shape.AddSublayer(pa.ToShape());
                }
                else if (item is CirclePath cir)
                {
                    shape.AddSublayer(cir.ToShape());
                }
                else if (item is RectanglePath rec)
                {
                    shape.AddSublayer(rec.ToShape());
                }
                else if (item is LinePath line)
                {
                    shape.AddSublayer(line.ToShape());
                }
                else if (item is TextPath text)
                {
                    shape.AddSublayer(text.ToShape());
                }
            }
            return(shape);
        }
Example #8
0
        CAShapeLayer SetupProgressLayer()
        {
            _progressLayer = new CAShapeLayer
            {
                ShadowOpacity = ProgressButtonFloats.ProgressLayerShadowOpacity.AsResourceFloat(),
                FillColor     = ProgressButtonColors.FillProgressColor.AsResourceCgColor()
            };
            _progressLayer.Bind("path", _progressButtonLayer, "curPath", null);
            var checkMarkLayer = new CALayer();

            checkMarkLayer.Frame = ProgressButtonRects.CheckMarkFrame.AsRect();
            checkMarkLayer.Bind("contents", _progressButtonLayer, "curImage", null);
            _progressLayer.AddSublayer(checkMarkLayer);
            return(_progressLayer);
        }
Example #9
0
        protected Cap(
            CGImage icon,
            Func <nfloat, nfloat> scale,
            nfloat iconHeight,
            nfloat iconWidth,
            ShadowDirection shadowDirection)
        {
            this.shadowDirection = shadowDirection;
            var center = new CGPoint(0, 0);

            var outerPath = new UIBezierPath();

            outerPath.AddArc(center, scale(outerRadius), 0, (nfloat)Math.FullCircle, false);

            Path = outerPath.CGPath;

            var innerPath = new UIBezierPath();

            innerPath.AddArc(center, scale(innerRadius), 0, (nfloat)Math.FullCircle, false);

            var circleLayer = new CAShapeLayer {
                Path = innerPath.CGPath, FillColor = ColorAssets.Background.CGColor
            };

            var imageFrame = new CGRect(
                x: center.X - scale(iconWidth) / 2f,
                y: center.Y - scale(iconHeight) / 2f,
                width: scale(iconWidth),
                height: scale(iconHeight));

            var maskLayer = new CALayer {
                Contents = icon, Frame = new CGRect(0, 0, scale(iconWidth), scale(iconHeight))
            };

            imageLayer = new CALayer {
                Mask = maskLayer, Frame = imageFrame
            };

            circleLayer.AddSublayer(imageLayer);

            AddSublayer(circleLayer);

            ShadowColor   = ColorAssets.Separator.CGColor;
            ShadowRadius  = 0.0f;
            ShadowPath    = Path;
            ShadowOpacity = 1.0f;
        }
        private void Setup()
        {
            CellRadiusRatio = 0.75f;
            Cells           = new List <LiquidFloatingCell>();
            BackgroundColor = UIColor.Clear;
            ClipsToBounds   = false;
            Responsible     = true;

            baseView.Setup(this);
            AddSubview(baseView);

            liquidView.UserInteractionEnabled = false;
            AddSubview(liquidView);

            liquidView.Layer.AddSublayer(circleLayer);
            circleLayer.AddSublayer(plusLayer);
        }
 private void Setup()
 {
     CellRadiusRatio = 0.75f;
     Cells           = new List <LiquidFloatingCell>();
     BackgroundColor = UIColor.Clear;
     ClipsToBounds   = false;
     Responsible     = true;
     _baseView.Setup(this);
     // ISSUE: reference to a compiler-generated method
     AddSubview(_baseView);
     _liquidView.UserInteractionEnabled = false;
     // ISSUE: reference to a compiler-generated method
     AddSubview(_liquidView);
     // ISSUE: reference to a compiler-generated method
     _liquidView.Layer.AddSublayer(_circleLayer);
     // ISSUE: reference to a compiler-generated method
     _circleLayer.AddSublayer(_plusLayer);
 }
Example #12
0
        void Draw(CGPoint [] points)
        {
            var newLayer = new CAShapeLayer {
                StrokeColor = UIColor.Red.CGColor,
                LineWidth   = 2
            };

            var path = new UIBezierPath();

            path.MoveTo(points[0]);
            foreach (var point in points)
            {
                path.AddLineTo(point);
                path.MoveTo(point);
            }
            path.AddLineTo(points[0]);
            newLayer.Path = path.CGPath;

            shapeLayer.AddSublayer(newLayer);
        }
		private void DrawMarker (CGRect rowRect, float position)
		{
			if (Layer.Sublayers != null) {
				Layer.Sublayers = new CALayer[0];
			}

			var visibleRect = Layer.Bounds;
			var currentTimeRect = visibleRect;

			// The red band of the timeMaker will be 7 pixels wide
			currentTimeRect.X = 0f;
			currentTimeRect.Width = 7f;

			var timeMarkerRedBandLayer = new CAShapeLayer ();
			timeMarkerRedBandLayer.Frame = currentTimeRect;
			timeMarkerRedBandLayer.Position = new CGPoint (rowRect.X, Bounds.Height / 2f);

			var linePath = CGPath.FromRect (currentTimeRect);
			timeMarkerRedBandLayer.FillColor = UIColor.FromRGBA (1.00f, 0.00f, 0.00f, 0.50f).CGColor;

			timeMarkerRedBandLayer.Path = linePath;

			currentTimeRect.X = 0f;
			currentTimeRect.Width = 1f;

			CAShapeLayer timeMarkerWhiteLineLayer = new CAShapeLayer ();
			timeMarkerWhiteLineLayer.Frame = currentTimeRect;
			timeMarkerWhiteLineLayer.Position = new CGPoint (3f, Bounds.Height / 2f);

			CGPath whiteLinePath = CGPath.FromRect (currentTimeRect);
			timeMarkerWhiteLineLayer.FillColor = UIColor.FromRGBA (1.00f, 1.00f, 1.00f, 1.00f).CGColor;
			timeMarkerWhiteLineLayer.Path = whiteLinePath;

			timeMarkerRedBandLayer.AddSublayer (timeMarkerWhiteLineLayer);
			CABasicAnimation scrubbingAnimation = new CABasicAnimation ();
			scrubbingAnimation.KeyPath = "position.x";

			scrubbingAnimation.From = new NSNumber (HorizontalPositionForTime (CMTime.Zero));
			scrubbingAnimation.To = new NSNumber (HorizontalPositionForTime (duration));
			scrubbingAnimation.RemovedOnCompletion = false;
			scrubbingAnimation.BeginTime = 0.000000001;
			scrubbingAnimation.Duration = duration.Seconds;
			scrubbingAnimation.FillMode = CAFillMode.Both;
			timeMarkerRedBandLayer.AddAnimation (scrubbingAnimation, null);
			if (Player != null) {
				Console.WriteLine ("Duration in  seconds - " + Player.CurrentItem.Asset.Duration.Seconds);
				var syncLayer = new AVSynchronizedLayer () {
					PlayerItem = Player.CurrentItem,
				};
				syncLayer.AddSublayer (timeMarkerRedBandLayer);
				Layer.AddSublayer (syncLayer);
			}
		}
        public static void AddBadge(this UIBarButtonItem barButtonItem, string text, UIColor backgroundColor, UIColor textColor, bool filled = true, float fontSize = 11.0f)
        {
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            CGPoint offset = CGPoint.Empty;

            if (backgroundColor == null)
            {
                backgroundColor = UIColor.Red;
            }

            var font = UIFont.SystemFontOfSize(fontSize);

            if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0))
            {
                font = UIFont.MonospacedDigitSystemFontOfSize(fontSize, UIFontWeight.Regular);
            }

            var view   = barButtonItem.ValueForKey(new NSString(@"view")) as UIView;
            var bLayer = GetBadgeLayer(barButtonItem);

            bLayer?.RemoveFromSuperLayer();

            var badgeSize = text.StringSize(font);

            var height = badgeSize.Height;
            var width  = badgeSize.Width + 2; /* padding */

            //make sure we have at least a circle
            if (width < height)
            {
                width = height;
            }

            //x position is offset from right-hand side
            var x = view.Frame.Width - width + offset.X;

            var badgeFrame = new CGRect(new CGPoint(x: x, y: offset.Y), size: new CGSize(width: width, height: height));

            bLayer = new CAShapeLayer();
            DrawRoundedRect(bLayer, badgeFrame, 7.0f, backgroundColor, filled);
            view.Layer.AddSublayer(bLayer);

            // Initialiaze Badge's label
            var label = new CATextLayer
            {
                String            = text,
                TextAlignmentMode = CATextLayerAlignmentMode.Center,
                FontSize          = font.PointSize,
                Frame             = badgeFrame,
                ForegroundColor   = filled ? textColor.CGColor : UIColor.White.CGColor,
                BackgroundColor   = UIColor.Clear.CGColor,
                ContentsScale     = UIScreen.MainScreen.Scale
            };

            label.SetFont(CGFont.CreateWithFontName(font.Name));
            bLayer.AddSublayer(label);

            // Save Badge as UIBarButtonItem property
            objc_setAssociatedObject(barButtonItem.Handle, BadgeKey.Handle, bLayer.Handle, AssociationPolicy.RETAIN_NONATOMIC);
        }
    public ODRefreshControl (UIScrollView scrollView, ODRefreshControlLayout layout = ODRefreshControlLayout.Vertical, UIView activity = null)
        : base (
            (layout == ODRefreshControlLayout.Vertical)
            ? new RectangleF (0, (-TotalViewHeight - scrollView.ContentInset.Top), scrollView.Bounds.Width, TotalViewHeight)
            : new RectangleF ((-TotalViewHeight - scrollView.ContentInset.Left), 0, TotalViewHeight, scrollView.Bounds.Height)
            )
    {
        ScrollView = scrollView;
        OriginalContentInset = scrollView.ContentInset;

        _vertical = (layout == ODRefreshControlLayout.Vertical);

        AutoresizingMask = (_vertical)
            ? UIViewAutoresizing.FlexibleWidth
            : UIViewAutoresizing.FlexibleHeight;
        
        ScrollView.AddSubview (this);
        ScrollView.AddObserver (this, new NSString ("contentOffset"), NSKeyValueObservingOptions.New, IntPtr.Zero);
        ScrollView.AddObserver (this, new NSString ("contentInset"), NSKeyValueObservingOptions.New, IntPtr.Zero);
        
        _activity = activity ?? new UIActivityIndicatorView (UIActivityIndicatorViewStyle.Gray);
        _activity.Center = new PointF ((float) Math.Floor (Bounds.Width / 2.0f), (float) Math.Floor (Bounds.Height / 2.0f));
        
        _activity.AutoresizingMask = (_vertical)
            ? UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin
            : UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleBottomMargin;
        
        _activity.Alpha = 1;
        
        if (_activity is UIActivityIndicatorView) {
            ((UIActivityIndicatorView) _activity).StartAnimating ();
        }
        AddSubview (_activity);
        
        _refreshing = false;
        _canRefresh = true;
        
        _ignoreInset = false;
        _ignoreOffset = false;
        _didSetInset = false;
        _hasSectionHeaders = false;

        _tintColor = UIColor.FromRGB (155, 162, 172);
        _shadowColor = UIColor.Black;
        _highlightColor = UIColor.White.ColorWithAlpha (.2f);

        _shapeLayer = new CAShapeLayer {
            FillColor = _tintColor.CGColor,
            StrokeColor = UIColor.DarkGray.ColorWithAlpha (.5f).CGColor,
            LineWidth = .5f,
            ShadowColor = _shadowColor.CGColor,
            ShadowOffset = new SizeF (0, 1),
            ShadowOpacity = .4f,
            ShadowRadius = .5f
        };
        
        Layer.AddSublayer (_shapeLayer);
        
        _arrowLayer = new CAShapeLayer {
            StrokeColor = UIColor.DarkGray.ColorWithAlpha (.5f).CGColor,
            LineWidth = .5f,
            FillColor = UIColor.White.CGColor
        };
        
        _shapeLayer.AddSublayer (_arrowLayer);

        _highlightLayer = new CAShapeLayer {
            FillColor = _highlightColor.CGColor
        };

        _shapeLayer.AddSublayer (_highlightLayer);

        if (!_vertical) {
            // Highlight layer is currently not shown in horizontal mode
            // because it has a wrong path.

            // It should instead be drawn all the way from left to right circle.
            // Feel free to work on it!

            _highlightLayer.RemoveFromSuperLayer ();
        }
    }
        public ODRefreshControl(UIScrollView scrollView, UIActivityIndicatorView activity)
            : base(new RectangleF(0, -1 * (kTotalViewHeight + scrollView.ContentInset.Top), scrollView.Frame.Size.Width, kTotalViewHeight))
        {
            this.scrollView = scrollView;
            this.originalContentInset = this.scrollView.ContentInset;

            this.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
            this.scrollView.AddSubview(this);
            this.scrollView.AddObserver(this, new NSString("contentOffset"), NSKeyValueObservingOptions.New, IntPtr.Zero);
            this.scrollView.AddObserver(this, new NSString("contentInset"), NSKeyValueObservingOptions.New, IntPtr.Zero);

            if (activity == null)
                activity = new UIActivityIndicatorView(UIActivityIndicatorViewStyle.Gray);
            this.activity = activity;
            this.activity.Center = new PointF((float)Math.Floor(this.Frame.Size.Width / 2), (float)Math.Floor(this.Frame.Size.Height / 2));
            this.activity.AutoresizingMask = UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin;
            this.activity.Alpha = 0;
            this.activity.StartAnimating();
            this.AddSubview(this.activity);

            _refreshing = false;
            _canRefresh = true;
            _ignoreInset = false;
            _ignoreOffset = false;
            _didSetInset = false;
            _hasSectionHeaders = false;
            TintColor = UIColor.FromRGBA(155 / 255, 162 / 255, 172 / 255, 1.0f);

            _shapeLayer = new CAShapeLayer();
            _shapeLayer.FillColor = TintColor.CGColor;
            _shapeLayer.StrokeColor = UIColor.DarkGray.ColorWithAlpha(0.5f).CGColor;
            _shapeLayer.LineWidth = 0.5f;
            _shapeLayer.ShadowColor = UIColor.Black.CGColor;
            _shapeLayer.ShadowOffset = new SizeF(0, 1);
            _shapeLayer.ShadowOpacity = 0.4f;
            _shapeLayer.ShadowRadius = 0.5f;
            this.Layer.AddSublayer(_shapeLayer);

            _arrowLayer = new CAShapeLayer();
            _arrowLayer.StrokeColor = UIColor.DarkGray.ColorWithAlpha(0.5f).CGColor;
            _arrowLayer.LineWidth = 0.5f;
            _arrowLayer.FillColor = UIColor.White.CGColor;
            _shapeLayer.AddSublayer(_arrowLayer);

            _highlightLayer = new CAShapeLayer();
            _highlightLayer.FillColor = UIColor.White.ColorWithAlpha(0.2f).CGColor;
            _shapeLayer.AddSublayer(_highlightLayer);
        }
Example #17
0
        private CALayer CreateLayer()
        {
            var path = this.GetPath();

            if (path == null)
            {
                return(null);
            }

            var pathBounds = path.PathBoundingBox;

            if (
                nfloat.IsInfinity(pathBounds.Left) ||
                nfloat.IsInfinity(pathBounds.Left)
                )
            {
                if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
                {
                    this.Log().Debug($"Ignoring path with invalid bounds {pathBounds}");
                }

                return(null);
            }

            var transform = CGAffineTransform.MakeIdentity();

            var scaleX = _scaleX;
            var scaleY = _scaleY;

            switch (this.Stretch)
            {
            case Stretch.Fill:
            case Stretch.None:
                break;

            case Stretch.Uniform:
                scaleX = (nfloat)Math.Min(_scaleX, _scaleY);
                scaleY = scaleX;
                break;

            case Stretch.UniformToFill:
                scaleX = (nfloat)Math.Max(_scaleX, _scaleY);
                scaleY = scaleX;
                break;
            }

            transform = CGAffineTransform.MakeScale(scaleX, scaleY);

            if (Stretch != Stretch.None)
            {
                // When stretching, we can't use 0,0 as the origin, but must instead
                // use the path's bounds.
                transform.Translate(-pathBounds.Left * scaleX, -pathBounds.Top * scaleY);
            }

            if (!ShouldPreserveOrigin)
            {
                //We need to translate the shape to take in account the stroke thickness
                transform.Translate((nfloat)ActualStrokeThickness * 0.5f, (nfloat)ActualStrokeThickness * 0.5f);
            }

            if (nfloat.IsNaN(transform.x0) || nfloat.IsNaN(transform.y0) ||
                nfloat.IsNaN(transform.xx) || nfloat.IsNaN(transform.yy) ||
                nfloat.IsNaN(transform.xy) || nfloat.IsNaN(transform.yx)
                )
            {
                //transformedPath creation will crash natively if the transform contains NaNs
                throw new InvalidOperationException($"transform {transform} contains NaN values, transformation will fail.");
            }

            var colorFill    = Fill as SolidColorBrush ?? SolidColorBrushHelper.Transparent;
            var imageFill    = Fill as ImageBrush;
            var gradientFill = Fill as LinearGradientBrush;
            var stroke       = this.Stroke as SolidColorBrush ?? SolidColorBrushHelper.Transparent;

            var transformedPath = new CGPath(path, transform);
            var layer           = new CAShapeLayer()
            {
                Path        = transformedPath,
                StrokeColor = stroke.ColorWithOpacity,
                LineWidth   = (nfloat)ActualStrokeThickness,
            };

            if (colorFill != null)
            {
                layer.FillColor = colorFill.ColorWithOpacity;
            }

            if (imageFill != null)
            {
                var fillMask = new CAShapeLayer()
                {
                    Path  = path,
                    Frame = Bounds,
                    // We only use the fill color to create the mask area
                    FillColor = _Color.White.CGColor,
                };

                CreateImageBrushLayers(
                    layer,
                    imageFill,
                    fillMask
                    );
            }
            else if (gradientFill != null)
            {
                var fillMask = new CAShapeLayer()
                {
                    Path  = transformedPath,
                    Frame = Bounds,
                    // We only use the fill color to create the mask area
                    FillColor = _Color.White.CGColor,
                };

                var gradientLayer = gradientFill.GetLayer(Frame.Size);
                gradientLayer.Frame         = Bounds;
                gradientLayer.Mask          = fillMask;
                gradientLayer.MasksToBounds = true;
                layer.AddSublayer(gradientLayer);
            }

            if (StrokeDashArray != null)
            {
                var pattern = StrokeDashArray.Select(d => (global::Foundation.NSNumber)d).ToArray();

                layer.LineDashPhase   = 0;               // Starting position of the pattern
                layer.LineDashPattern = pattern;
            }

            return(layer);
        }
Example #18
0
        internal static void pulseExpandAnimation(CALayer layer, CALayer visualLayer, CGPoint point, nfloat width, nfloat height, Pulse pulse)
        {
            if (pulse.Animation != PulseAnimation.None)
            {
                nfloat n      = pulse.Animation == PulseAnimation.Center ? width < height ? width : height : width < height ? height : width;
                var    bLayer = new CAShapeLayer();
                var    pLayer = new CAShapeLayer();
                bLayer.AddSublayer(pLayer);
                pulse.Layers.Enqueue(bLayer);
                visualLayer.AddSublayer(bLayer);
                Animation.AnimationDisabled(() =>
                {
                    bLayer.Frame  = visualLayer.Bounds;
                    pLayer.Bounds = new CGRect(0, 0, n, n);
                    switch (pulse.Animation)
                    {
                    case PulseAnimation.Center:
                    case PulseAnimation.CenterWithBacking:
                    case PulseAnimation.CenterRadialBeyondBounds:
                        pLayer.Position = new CGPoint(width / 2, height / 2);
                        break;

                    default:
                        pLayer.Position = point;
                        break;
                    }
                    pLayer.CornerRadius    = n / 2;
                    pLayer.BackgroundColor = pulse.Color.ColorWithAlpha(pulse.Opacity).CGColor;
                    pLayer.Transform       = CATransform3D.MakeFromAffine(CGAffineTransform.MakeScale(0, 0));
                });
                bLayer.SetValueForKey(NSObject.FromObject(false), new NSString("animated"));

                var duration = pulse.Animation == PulseAnimation.Center ? 0.16125 : 0.325;
                switch (pulse.Animation)
                {
                case PulseAnimation.CenterWithBacking:
                case PulseAnimation.Backing:
                case PulseAnimation.AtPointWithBacking:
                    bLayer.AddAnimation(Animation.BackgroundColor(pulse.Color.ColorWithAlpha(pulse.Opacity / 2), duration), null);
                    break;

                default:
                    break;
                }
                switch (pulse.Animation)
                {
                case PulseAnimation.Center:
                case PulseAnimation.CenterWithBacking:
                case PulseAnimation.CenterRadialBeyondBounds:
                case PulseAnimation.AtPoint:
                case PulseAnimation.AtPointWithBacking:
                    pLayer.AddAnimation(Animation.Scale(1, duration), null);
                    break;

                default:
                    break;
                }
                Animation.Delay(duration, () =>
                {
                    bLayer.SetValueForKey(NSObject.FromObject(true), new NSString("animated"));
                });
            }
        }