Deepak
Deepak

Reputation: 575

How can I restrict gestures are working only draw line area itself in Xamarin.Ios

enter image description here

What I have tried is

  CAShapeLayer shapeLayer = new CAShapeLayer();

  shapeLayer.Path = new CGPath();

  shapeLayer.Path.AddLines(new CGPoint [] { startingPoint, endingPoint});

  shapeLayer.StrokeColor = UIColor.Blue.CGColor;

  shapeLayer.LineWidth = (System.nfloat)3.0;

  shapeLayer.FillColor = UIColor.Clear.CGColor;

  var width = Math.Abs(latestPoint.X - initioalPoint.X) + initioalPoint.X;

  var height = Math.Abs(latestPoint.Y - initioalPoint.Y) + initioalPoint.Y;

  var padding = 10;

  shapeLayer.Frame = new CGRect(5, 5, width + padding - 2, height + padding - 2);


  var view = new UIView();

  view.Layer.AddSubLayer(shapeLayer);


  CanvasView canvasDrawLine = new CanvasView(subView);

  canvasDrawLine.Frame = new CGRect(5, 5, width + padding, height + padding[![enter image description here][1]][1]);

  Public Class CanvasView : UIView
 {
    public CanvasView(drawline)
    {
       drawLine.ClipsToBounds = true;

       var width = Math.Abs(endingPoint.X - initioalPoint.X) + initioalPoint.X;

       var height = Math.Abs(endingPoint.Y - initioalPoint.Y) + initioalPoint.Y;

        var padding = 10;

        drawLine.Frame = new CGRect(5,5, width + padding, height + padding);

        this.AddSubview(drawLine);

        setUpGestures();
    }
 }

 scrollView.AddSubview(canvasDrawLine );

when I set the width and height of the frame default, my gestures not working properly, for example If I have apply a pan gesture to the draw line even out of the draw line also pan gesture is working.How can I restrict all gestures are working only draw line area.

Upvotes: 0

Views: 89

Answers (1)

PlusInfosys
PlusInfosys

Reputation: 3436

you can calculate bounds of your area covered by line :

CoreGraphics.CGPoint initialPoint = new CoreGraphics.CGPoint(1, 2);
            CoreGraphics.CGPoint latestPoint = new CoreGraphics.CGPoint(1, 2);
            var width = Math.Abs(latestPoint.X - initialPoint.X);
            var height = Math.Abs(latestPoint.X - initialPoint.X);
            var padding = 10;

            subView.Frame = new CGRect(0, 0, width+padding, height+padding);

I have created sample code with simple view controller class -- Have assigned red colour to view containing line. And its working perfect. gesture recogniser is working on red part only --where line is.

public partial class ViewController : UIViewController
{
    protected ViewController(IntPtr handle) : base(handle)
    {
        // Note: this .ctor should not contain any initialization logic.
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        CGPoint initioalPoint = new CGPoint(12, 12);
        CGPoint latestPoint = new CGPoint(80, 45);
        var shapeLayer = new CAShapeLayer();

        shapeLayer.Path = new CGPath();

        shapeLayer.Path.AddLines(new CGPoint[] { initioalPoint, latestPoint });

        shapeLayer.StrokeColor = UIColor.Blue.CGColor;

        shapeLayer.LineWidth = (System.nfloat)3.0;

        shapeLayer.FillColor = UIColor.Clear.CGColor;

        var subView = new UIView();
        var width = Math.Abs(latestPoint.X - initioalPoint.X) + initioalPoint.X;
        var height = Math.Abs(latestPoint.Y - initioalPoint.Y) + initioalPoint.Y;
        var padding = 10;

        subView.Frame = new CGRect(5, 5, width + padding, height + padding);
        subView.Layer.AddSublayer(shapeLayer);
        shapeLayer.Frame = new CGRect(5, 5, width + padding-2, height + padding-2);
        subView.BackgroundColor = UIColor.Red;


        var mainView = new UIView();

        mainView.Frame = new CGRect(40, 40, width + padding+200, height + padding+200);
        mainView.AddSubview(subView);
        mainView.BackgroundColor = UIColor.Yellow;

        this.View.AddSubview(mainView);
        UITapGestureRecognizer tap = new UITapGestureRecognizer();
        tap.AddTarget((obj) => {new  UIAlertView("tapped","",null,"ok",null).Show();});
        subView.AddGestureRecognizer(tap);
        //UITapGestureRecognizer tap  = new UITapGestureRecognizer(() => Console.WriteLine("tap"));


        // Perform any additional setup after loading the view, typically from a nib.
    }

    public override void DidReceiveMemoryWarning()
    {
        base.DidReceiveMemoryWarning();
        // Release any cached data, images, etc that aren't in use.
    }
}

///To check if Touch Point is inside Given Polygon::

    CGPoint pt1 = new CGPoint(initioalPoint.X - 50, initioalPoint.Y - 50);
    CGPoint pt2 = new CGPoint(initioalPoint.X + 50, initioalPoint.Y - 50);
    CGPoint pt3 = new CGPoint(latestPoint.X - 50, latestPoint.Y + 50);
    CGPoint pt4 = new CGPoint(latestPoint.X + 50, latestPoint.Y + 50);

    UIBezierPath path = new UIBezierPath();
    path.MoveTo(pt1);
    path.AddLineTo(pt2);
    path.AddLineTo(pt3);
    path.AddLineTo(pt4);
    path.AddLineTo(pt1);

    if (!path.ContainsPoint(ToucPoint))
        return;

Upvotes: 1

Related Questions