Deepak
Deepak

Reputation: 575

Gestures are not working when CAShapeLayer is added as a SubLayer of UIView in Xamarin.Ios

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;

In the above code I have draw a line using starting point and endingPoint. Now these shapeLayer is added as Sublayer of another view.

      var view = new UIView();

      view.Layer.AddSubLayer(shapeLayer);

Now these view as added as a subview of another view i.e CanvasView. In these we are implement the all gestures functionality.

      CanvasView canvasDrawLine = new CanvasView(subView);

here is the canvasView :

      Public Class CanvasView : UIView
     {
        public CanvasView(drawline)
        {
           this.AddSubview(drawLine);

           setUpGestures();
        }
     }

I have added all type of gestures in 'setUpGestures' method. Now these CanvasView is added as a subview of 'scrollView'. scrollView is the superView.

       scrollView.AddSubview(canvasDrawLine );

What my problem is after line draw, gestures are not working. Thanks in advance.

Upvotes: 0

Views: 202

Answers (1)

PlusInfosys
PlusInfosys

Reputation: 3436

Please set frame of your view when you add it to superview with AddSubview. When you add view to superview, It may display its content which is outside its bounds but Because its frame is (0,0) -- It don't have any touch area. and thats why you cant get gesture events.

1) Add canvasDrawLine.ClipsToBounds = true and drawLine.ClipsToBounds = true. This is to check what is bounds for your subviews: after doing this, It will display data only inside the bounds of view. So if frame of view is 0,0-- it will not display any data. ClipsToBounds Clip the view out side its bounds (Frames width and height)..

2) Assign proper frame to Both views.

CanvasDrawLine.frame = new CoreGraphics.CGRect(0, 0, 100, 100); //edit it according to your requirement,

drawLine.frame =  new CoreGraphics.CGRect(0, 0, 100, 100); //edit it according to your requirement,

For testing you can use above frame and verify gestures. You can assign proper frame according to your requirement then.

Upvotes: 1

Related Questions