public override void QuadraticBezierTo(Point point1, Point point2, bool isStroked, bool isSmoothJoin) { #if XAMARIN_IOS_UNIFIED || XAMARIN_IOS bezierPath.AddQuadCurveToPoint(point2, point1); #elif XAMARIN_ANDROID var physicalPoint1 = LogicalToPhysicalNoRounding(point1); var physicalPoint2 = LogicalToPhysicalNoRounding(point2); bezierPath.QuadTo((float)physicalPoint1.X, (float)physicalPoint1.Y, (float)physicalPoint2.X, (float)physicalPoint2.Y); #endif _points.Add(point2); }
async Task animateView(UIView view) { var size = view.Frame.Size; var grow = new SizeF((float)size.Width * 1.7f, (float)size.Height * 1.7f); var shrink = new SizeF((float)size.Width * .4f, (float)size.Height * .4f); TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool> (); //Set the animation path var pathAnimation = CAKeyFrameAnimation.GetFromKeyPath("position"); pathAnimation.CalculationMode = CAAnimation.AnimationPaced; pathAnimation.FillMode = CAFillMode.Forwards; pathAnimation.RemovedOnCompletion = false; pathAnimation.Duration = .5; UIBezierPath path = new UIBezierPath (); path.MoveTo (view.Center); path.AddQuadCurveToPoint (new CGPoint (290, 34), new CGPoint(view.Center.X,View.Center.Y)); pathAnimation.Path = path.CGPath; //Set size change var growAnimation = CABasicAnimation.FromKeyPath("bounds.size"); growAnimation.To = NSValue.FromSizeF (grow); growAnimation.FillMode = CAFillMode.Forwards; growAnimation.Duration = .1; growAnimation.RemovedOnCompletion = false; var shrinkAnimation = CABasicAnimation.FromKeyPath("bounds.size"); shrinkAnimation.To = NSValue.FromSizeF (shrink); shrinkAnimation.FillMode = CAFillMode.Forwards; shrinkAnimation.Duration = .4; shrinkAnimation.RemovedOnCompletion = false; shrinkAnimation.BeginTime = .1; CAAnimationGroup animations = new CAAnimationGroup (); animations.FillMode = CAFillMode.Forwards; animations.RemovedOnCompletion = false; animations.Animations = new CAAnimation[] { pathAnimation, growAnimation, shrinkAnimation, }; animations.Duration = .5; animations.AnimationStopped += (sender, e) => { tcs.TrySetResult(true); }; view.Layer.AddAnimation (animations,"movetocart"); NSTimer.CreateScheduledTimer (.5, (timer) => view.RemoveFromSuperview ()); await tcs.Task; }
public override void QuadraticBezierTo(Point point1, Point point2, bool isStroked, bool isSmoothJoin) { #if __IOS__ bezierPath.AddQuadCurveToPoint(point2, point1); #elif __MACOS__ // Convert a Quadratic Curve to cubic curve to draw it. // https://stackoverflow.com/a/52569210/1771254 var startPoint = bezierPath.CurrentPoint; var endPoint = point1; var controlPoint1 = new CGPoint(startPoint.X + ((point2.X - startPoint.X) * 2.0 / 3.0), startPoint.Y + (point2.Y - startPoint.Y) * 2.0 / 3.0); var controlPoint2 = new CGPoint(endPoint.X + ((point2.X - endPoint.X) * 2.0 / 3.0), endPoint.Y + (point2.Y - endPoint.Y) * 2.0 / 3.0); bezierPath.CurveTo(point1, controlPoint1, controlPoint2); #elif __ANDROID__ bezierPath.QuadTo((float)point1.X, (float)point1.Y, (float)point2.X, (float)point2.Y); #elif __SKIA__ bezierPath.Geometry.QuadTo((float)point1.X, (float)point1.Y, (float)point2.X, (float)point2.Y); #endif _points.Add(point2); }
public static UIImage ToTransformedCorners(UIImage source, double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio) { double sourceWidth = source.Size.Width; double sourceHeight = source.Size.Height; double desiredWidth = sourceWidth; double desiredHeight = sourceHeight; double desiredRatio = cropWidthRatio / cropHeightRatio; double currentRatio = sourceWidth / sourceHeight; if (currentRatio > desiredRatio) desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio); else if (currentRatio < desiredRatio) desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio); topLeftCornerSize = topLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; topRightCornerSize = topRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomLeftCornerSize = bottomLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomRightCornerSize = bottomRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; float cropX = (float)((sourceWidth - desiredWidth) / 2); float cropY = (float)((sourceHeight - desiredHeight) / 2); UIGraphics.BeginImageContextWithOptions(new CGSize(desiredWidth, desiredHeight), false, (nfloat)0.0); try { using (var context = UIGraphics.GetCurrentContext()) { context.BeginPath(); using (var path = new UIBezierPath()) { // TopLeft if (cornersTransformType.HasFlag(CornerTransformType.TopLeftCut)) { path.MoveTo(new CGPoint(0, topLeftCornerSize)); path.AddLineTo(new CGPoint(topLeftCornerSize, 0)); } else if (cornersTransformType.HasFlag(CornerTransformType.TopLeftRounded)) { path.MoveTo(new CGPoint(0, topLeftCornerSize)); path.AddQuadCurveToPoint(new CGPoint(topLeftCornerSize, 0), new CGPoint(0, 0)); } else { path.MoveTo(new CGPoint(0, 0)); } // TopRight if (cornersTransformType.HasFlag(CornerTransformType.TopRightCut)) { path.AddLineTo(new CGPoint(desiredWidth - topRightCornerSize, 0)); path.AddLineTo(new CGPoint(desiredWidth, topRightCornerSize)); } else if (cornersTransformType.HasFlag(CornerTransformType.TopRightRounded)) { path.AddLineTo(new CGPoint(desiredWidth - topRightCornerSize, 0)); path.AddQuadCurveToPoint(new CGPoint(desiredWidth, topRightCornerSize), new CGPoint(desiredWidth, 0)); } else { path.AddLineTo(new CGPoint(desiredWidth ,0)); } // BottomRight if (cornersTransformType.HasFlag(CornerTransformType.BottomRightCut)) { path.AddLineTo(new CGPoint(desiredWidth, desiredHeight - bottomRightCornerSize)); path.AddLineTo(new CGPoint(desiredWidth - bottomRightCornerSize, desiredHeight)); } else if (cornersTransformType.HasFlag(CornerTransformType.BottomRightRounded)) { path.AddLineTo(new CGPoint(desiredWidth, desiredHeight - bottomRightCornerSize)); path.AddQuadCurveToPoint(new CGPoint(desiredWidth - bottomRightCornerSize, desiredHeight), new CGPoint(desiredWidth, desiredHeight)); } else { path.AddLineTo(new CGPoint(desiredWidth, desiredHeight)); } // BottomLeft if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftCut)) { path.AddLineTo(new CGPoint(bottomLeftCornerSize, desiredHeight)); path.AddLineTo(new CGPoint(0, desiredHeight - bottomLeftCornerSize)); } else if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftRounded)) { path.AddLineTo(new CGPoint(bottomLeftCornerSize, desiredHeight)); path.AddQuadCurveToPoint(new CGPoint(0, desiredHeight - bottomLeftCornerSize), new CGPoint(0, desiredHeight)); } else { path.AddLineTo(new CGPoint(0, desiredHeight)); } path.ClosePath(); context.AddPath(path.CGPath); context.Clip(); } var drawRect = new CGRect(-cropX, -cropY, sourceWidth, sourceHeight); source.Draw(drawRect); var modifiedImage = UIGraphics.GetImageFromCurrentImageContext(); return modifiedImage; } } finally { UIGraphics.EndImageContext(); } }