Book Image

iOS Development with Xamarin Cookbook

By : Dimitrios Tavlikos (USD)
Book Image

iOS Development with Xamarin Cookbook

By: Dimitrios Tavlikos (USD)

Overview of this book

Table of Contents (22 chapters)
iOS Development with Xamarin Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using physics in UI elements


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.

Getting ready

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.

How to do it...

Perform the following steps:

  1. Add the following fields in the ViewPhysicsAppViewController class:

    private RectangleF imageRect;
    private UIDynamicAnimator animator;
  2. 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);
  3. Next, in the ViewDidLoad method again, add the following button handlers:

    this.btnDrop.TouchUpInside += (sender, e) => {
     ...