예제 #1
0
        public override void Draw(CGRect rect)
        {
            base.Draw(rect);
            CGGradient gradient = null;

            using (CGColorSpace rgb = CGColorSpace.CreateDeviceRGB())
            {
                gradient = new CGGradient(rgb, new CGColor[]
                {
                    _startColor.ToCGColor(),
                    _middleColor.ToCGColor(),
                    _endColor.ToCGColor()
                });
            }
            var start = new CGPoint(0, 0);
            var end   = new CGPoint(rect.Width, rect.Height);

            for (var i = 0; i < Subviews.Length; i++)
            {
                var child = Subviews[i];
                if (child is FrameRenderer_iOS gFrame)
                {
                    gFrame.Gradient = gradient;
                    gFrame.Start    = start;
                    gFrame.End      = end;
                }
            }
        }
예제 #2
0
        public void SetColor(Xamarin.Forms.Color c)
        {
            var clr = c.ToCGColor();

#if MONOMAC
            _c.SetFillColorWithColor(cgcol);
            _c.SetStrokeColorWithColor(cgcol);
#else
            _c.SetFillColor(clr);
            _c.SetStrokeColor(clr);
#endif
        }
        // this will draw background and circular border then it will cut the image to a circle inside the border and padding
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            // the control as it exists in the protable project
            formControl = (SelectImageButton)sender;
            // the click events form the forms control to be added to the end of the long click
            buttonevents = formControl.ClickEvents;
            // setting the colors for use in ther renderer outside of the property changed event
            iosBackgroundColor     = formControl.BackgroundColor.ToCGColor();
            iosBorderColor         = formControl.BorderColor.ToCGColor();
            iosBorderWidth         = formControl.BorderWidth;
            iosHoldBackgroundColor = formControl.HoldBackgroundColor.ToCGColor();
            iosHoldBorderColor     = formControl.HoldBorderColor.ToCGColor();
            iosHoldBorderWidth     = formControl.HoldBorderWidth;
            // background
            Xamarin.Forms.Color bgcolor = formControl.BackgroundColor;
            Layer.BackgroundColor = bgcolor.ToCGColor();
            // border
            Layer.BorderColor = formControl.BorderColor.ToCGColor();
            Layer.BorderWidth = formControl.BorderWidth;

            Layer.CornerRadius = (float)formControl.Width / 2;
            // setup for a long press gesture
            UILongPressGestureRecognizer holdGesture = new UILongPressGestureRecognizer(holdPress);

            holdGesture.MinimumPressDuration = 0.0;
            this.AddGestureRecognizer(holdGesture);
            // this need to check that the control is actually loaded as it can call this method without the control present
            if (formControl != null)
            {
                // a top left and bottom right adjustment for the clipping border
                float TLborderControl = 0;
                float BRborderControl = formControl.BorderWidth;

                // a rectangle made usiing the bounds and borders of the control that we will use the cut an elipse with
                CGRect clipRect = new CGRect();

                clipRect.X      = (float)0 + TLborderControl;
                clipRect.Y      = (float)0 + TLborderControl;
                clipRect.Width  = (float)Element.Bounds.Width;
                clipRect.Height = (float)Element.Bounds.Height;

                CAShapeLayer clipShapeLayer = new CAShapeLayer();
                CGPath       clipPath       = new CGPath();

                clipPath.AddEllipseInRect(clipRect);
                clipShapeLayer.Path = clipPath;
                Layer.Mask          = clipShapeLayer;
            }
        }
예제 #4
0
        void SetOverlay(Xamarin.Forms.Color color)
        {
            var formsImage = (Xamarin.Forms.Image)Element;

            if (formsImage?.Source == null)
            {
                return;
            }

            try
            {
                UIImage image        = ((UIImageView)Control).Image;
                UIImage coloredImage = null;
                UIGraphics.BeginImageContextWithOptions(image.Size, false, 0.0f);
                using (CGContext context = UIGraphics.GetCurrentContext())
                {
                    context.TranslateCTM(0, image.Size.Height);
                    context.ScaleCTM(1.0f, -1.0f);

                    //var rect = new RectangleF(0, 0, (float)(image.Size.Width * image.CurrentScale), (float)(image.Size.Height * image.CurrentScale));
                    var rect = new RectangleF(0, 0, (float)(image.Size.Width), (float)(image.Size.Height));

                    // draw image, (to get transparancy mask)
                    context.SetBlendMode(CGBlendMode.Normal);
                    context.DrawImage(rect, image.CGImage);

                    // draw the color using the sourcein blend mode so its only draw on the non-transparent pixels
                    context.SetBlendMode(CGBlendMode.SourceIn);
                    context.SetFillColor(color.ToCGColor());
                    context.FillRect(rect);

                    coloredImage = UIGraphics.GetImageFromCurrentImageContext();
                    UIGraphics.EndImageContext();
                }

                ((UIImageView)Control).Image = coloredImage;
            }
            catch (ObjectDisposedException)
            {
                return;
            }
        }
 public static void UpdateBorder(this UIView nativeControl, Color color, int thickness)
 {
   nativeControl.Layer.BorderColor = color.ToCGColor();
   nativeControl.Layer.BorderWidth = thickness;
 }