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

Handling touch events


In this recipe, we will learn how to intercept and respond to user touches.

Getting ready

Create a new Single View Application in Xamarin Studio and name it TouchEventsApp.

How to do it...

Perform the following steps:

  1. Add a label to the view the controller.

  2. Enter the following code in the TouchEventsAppViewController class:

    public override void TouchesMoved (NSSet touches, UIEvent evt)
    {
      base.TouchesMoved (touches, evt);
      UITouch touch = touches.AnyObject as UITouch;
      UIColor currentColor = this.View.BackgroundColor;
      float red, green, blue, alpha;
      currentColor.GetRGBA(out red, out green, out blue, out alpha);
      PointF previousLocation = touch.PreviousLocationInView(this.View);
      PointF touchLocation = touch.LocationInView(this.View);
      if (previousLocation.X != touchLocation.X)
      {
        this.lblOutput.Text = "Changing background color...";
        float colorValue = touchLocation.X / this.View.Bounds.Width;
        this.View.BackgroundColor = UIColor.FromRGB(colorValue, colorValue...