public override void ViewDidLoad() { base.ViewDidLoad(); View.BackgroundColor = UIColor.White; animator = new UIDynamicAnimator(View); gravity = new UIGravityBehavior(); animator.AddBehavior(gravity); // Tap to add new item. View.AddGestureRecognizer(new UITapGestureRecognizer((gesture) => { PointF tapLocation = gesture.LocationInView(View); var item = new UIView(new RectangleF(PointF.Empty, sizeInitial)) { BackgroundColor = ColorHelpers.GetRandomColor(), Center = tapLocation, }; items.Enqueue(item); View.Add(item); gravity.AddItem(item); // Clean up old items so things don't get too leaky. if (items.Count > 20) { var oldItem = items.Dequeue(); oldItem.RemoveFromSuperview(); gravity.RemoveItem(oldItem); oldItem.Dispose(); } })); // Swipe to change gravity direction. View.AddGestureRecognizer(new UISwipeGestureRecognizer((gesture) => { gravity.GravityDirection = new CGVector(1, 0); }) { Direction = UISwipeGestureRecognizerDirection.Right, }); View.AddGestureRecognizer(new UISwipeGestureRecognizer((gesture) => { gravity.GravityDirection = new CGVector(-1, 0); }) { Direction = UISwipeGestureRecognizerDirection.Left, }); View.AddGestureRecognizer(new UISwipeGestureRecognizer((gesture) => { gravity.GravityDirection = new CGVector(0, -1); }) { Direction = UISwipeGestureRecognizerDirection.Up, }); View.AddGestureRecognizer(new UISwipeGestureRecognizer((gesture) => { gravity.GravityDirection = new CGVector(0, 1); }) { Direction = UISwipeGestureRecognizerDirection.Down, }); }
public override void ViewDidLoad() { base.ViewDidLoad(); View.BackgroundColor = UIColor.White; animator = new UIDynamicAnimator(View); item = new UIView(new RectangleF(new PointF(50f, 0f), new SizeF(50f, 50f))) { BackgroundColor = UIColor.Blue, }; View.Add(item); UIGravityBehavior gravity = new UIGravityBehavior(item); animator.AddBehavior(gravity); // Tap to bring item back. View.AddGestureRecognizer(new UITapGestureRecognizer((gesture) => { PointF tapLocation = gesture.LocationInView(View); // Must remove from gravity first or it will only flash to location // then continue animated from where it was. gravity.RemoveItem(item); item.Center = tapLocation; gravity.AddItem(item); })); }