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

Drawing lines and curves


In this recipe, we will implement custom drawing to draw two lines on a UIView class.

Getting ready

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

How to do it...

Perform the following steps:

  1. Add a new class to the project and name it DrawingView. Derive it from UIView as follows:

    public class DrawingView : UIView
  2. Add the following using directives in the DrawingView.cs file:

    using MonoTouch.CoreGraphics;
    using MonoTouch.UIKit;
    using System.Drawing;
  3. Add the following constructor to the class:

    public DrawingView(RectangleF frame) : base(frame) {}
  4. Override the Draw method of UIView and implement it with the following code:

    public override void Draw (RectangleF rect)
    {
      base.Draw (rect);
      Console.WriteLine("DrawingView draw!");
      CGContext context = UIGraphics.GetCurrentContext();
      context.SetLineWidth(5f);
      context.SetStrokeColorWithColor(UIColor.Green.CGColor);
      context.AddLines(new PointF[] { 
        new PointF(0f, this.Bounds.Height),
       ...