private UIImage CropImage(UIImage image, CGRect clipRect, int width, int height) { CGSize sz = new CGSize(width, height); if (UIScreen.MainScreen.Scale == 2.0) { UIGraphics.BeginImageContextWithOptions(sz, true, 0); } else { UIGraphics.BeginImageContext(sz); } UIGraphics.RectClip(clipRect); image.DrawAsPatternInRect(clipRect); UIImage newImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(newImage); }
public override void Draw(CGRect rect) { var boundsRect = Bounds; boundsRect.Height = drawHeight; if (SizeF.Equals(boundsRect.Size, lastBoundsSize)) { RegenerateImages(); lastBoundsSize = boundsRect.Size; } nfloat width = boundsRect.Size.Width; nfloat drawPercent = percent; if (((width - knobWidth) * drawPercent) < 3) { drawPercent = 0.0f; } if (((width - knobWidth) * drawPercent) > (width - knobWidth - 3)) { drawPercent = 1.0f; } if (endDate != DateTime.MinValue) { var interval = (endDate - DateTime.Now).TotalSeconds; if (interval < 0.0) { endDate = DateTime.MinValue; } else { if (percent == 1f) { drawPercent = (float)Math.Cos((interval / animationDuration) * (Math.PI / 2.0)); } else { drawPercent = 1.0f - (float)Math.Cos((interval / animationDuration) * (Math.PI / 2.0)); } PerformSelector(new Selector("setNeedsDisplay"), null, 0.0); } } CGContext context = UIGraphics.GetCurrentContext(); context.SaveState(); UIGraphics.PushContext(context); if (drawPercent != 1.0) { var sliderOffRect = boundsRect; sliderOffRect.Height = sliderOff.Size.Height; sliderOff.Draw(sliderOffRect); } if (drawPercent > 0.0f && drawPercent < 1.0f) { nfloat onWidth = knobWidth / 2 + ((width - knobWidth / 2) - knobWidth / 2) * drawPercent; var sourceRect = new CGRect(0, 0, onWidth * scale, sliderOn.Size.Height * scale); var drawOnRect = new CGRect(0, 0, onWidth, sliderOn.Size.Height); CGImage sliderOnSubImage = sliderOn.CGImage.WithImageInRect(sourceRect); context.SaveState(); context.ScaleCTM(1, -1); context.TranslateCTM(0, -drawOnRect.Height); context.DrawImage(drawOnRect, sliderOnSubImage); context.RestoreState(); } if (drawPercent == 1.0) { nfloat onWidth = sliderOn.Size.Width; var sourceRect = new CGRect(0, 0, onWidth * scale, sliderOn.Size.Height * scale); var drawOnRect = new CGRect(0, 0, onWidth, sliderOn.Size.Height); CGImage sliderOnSubImage = sliderOn.CGImage.WithImageInRect(sourceRect); context.SaveState(); context.ScaleCTM(1, -1); context.TranslateCTM(0, -drawOnRect.Height); context.DrawImage(drawOnRect, sliderOnSubImage); context.RestoreState(); } context.SaveState(); UIGraphics.PushContext(context); var insetClipRect = boundsRect.Inset(4, 4); UIGraphics.RectClip(insetClipRect); DrawUnderlayersInRect(rect, drawPercent * (boundsRect.Width - knobWidth), boundsRect.Width - knobWidth); UIGraphics.PopContext(); context.RestoreState(); context.ScaleCTM(1, -1); context.TranslateCTM(0, -boundsRect.Height); CGPoint location = boundsRect.Location; UIImage imageToDraw = knobImage; if (this.Highlighted) { imageToDraw = knobImageOff; } nfloat xlocation; if (drawPercent == 0.0f) { xlocation = location.X + 3 + (nfloat)Math.Round(drawPercent * (boundsRect.Width - knobWidth + 2)); } else { xlocation = location.X - 5 + (nfloat)Math.Round(drawPercent * (boundsRect.Width - knobWidth + 2)); xlocation = xlocation < 0.0f ? 0.0f : xlocation; } var rectToDraw = new CGRect(xlocation, location.Y + 9.5f, knobWidth, knobImage.Size.Height); context.DrawImage(rectToDraw, imageToDraw.CGImage); UIGraphics.PopContext(); context.RestoreState(); }