In this recipe, we will use UIKit Dynamics to add the properties of physics to an image view. The image view will drop from its initial position to the bottom of the screen, simulating the effect of an object dropping on the floor.
Create a new Single View Application in Xamarin Studio and name it ViewPhysicsApp
. Add UIImageView
and two buttons to the controller. We will also need an image to show in the image view.
Perform the following steps:
Add the following fields in the
ViewPhysicsAppViewController
class:private RectangleF imageRect; private UIDynamicAnimator animator;
Add the following code in the
ViewDidLoad
method:this.View.InsertSubviewBelow(this.imgView, this.btnReset); this.imageRect = this.imgView.Frame; this.imgView.Image = UIImage.FromFile("1.jpg"); this.animator = new UIDynamicAnimator(this.View);
Next, in the
ViewDidLoad
method again, add the following button handlers:this.btnDrop.TouchUpInside += (sender, e) => { ...