async Task animateView(UIView view) { var size = view.Frame.Size; var grow = new SizeF(size.Width * 1.7f, size.Height * 1.7f); var shrink = new SizeF(size.Width * .4f, 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 PointF(290, 34), new PointF(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, () => view.RemoveFromSuperview()); await tcs.Task; }
public override void ViewDidLoad() { base.ViewDidLoad(); // Perform any additional setup after loading the view, typically from a nib. button = new UIButton() { BackgroundColor = UIColor.FromHSBA(0.531f, 1, 1, 1), Frame = new RectangleF(0, 0, 140, 140), }; button.SetTitleColor(UIColor.White, UIControlState.Normal); button.SetTitle("Tap to start", UIControlState.Normal); button.Layer.CornerRadius = 70; button.TouchUpInside += delegate { var anim = button.PopAnimationForKey("bounce.me"); if (anim != null) { button.PopRemoveAnimationForKey("bounce.me"); button.SetTitle("Tap to start", UIControlState.Normal); } else { float size = 1; NSValue fromValue = NSValue.FromSizeF(new SizeF(size, size)); NSValue toValue = NSValue.FromSizeF(new SizeF(1.4f, 1.4f)); POPSpringAnimation bounce = POPSpringAnimation.AnimationWithPropertyNamed(POPMutableAnimatableProperty.POPViewScaleXY); bounce.SpringBounciness = 14; bounce.FromValue = fromValue; bounce.ToValue = toValue; // Note: CompletionBlock is much more reliable than Finished bounce.CompletionBlock = delegate(POPAnimation animation, bool finished) { if (finished) { // Auto-reverse var prevFrom = (animation as POPSpringAnimation).FromValue; (animation as POPSpringAnimation).FromValue = (animation as POPSpringAnimation).ToValue; (animation as POPSpringAnimation).ToValue = prevFrom; button.PopAddAnimation(animation, "bounce.me"); } }; button.PopAddAnimation(bounce, "bounce.me"); button.SetTitle("Tap to stop", UIControlState.Normal); } }; button.Center = View.Center; View.Add(button); }