drawing dashed line using CALayer

后端 未结 8 1381
执笔经年
执笔经年 2020-12-04 16:49

I was able to draw a dashed box, using the following code:

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
CGRect shapeRect = CGRectMake(0.0f, 0.0f, 200.0f,         


        
8条回答
  •  一向
    一向 (楼主)
    2020-12-04 17:22

    Below is the code snippet to draw a Dashed line in UIView (Xamarin iOS)

    Note: separatorView is my UIView which i need to show as Dashed

     public void ShowDottedLine()
     {
          var dashedLayer = new CAShapeLayer();
          var frameSize = separatorView.Frame.Size;
          var shapeRect = new CGRect(0, 0, frameSize.Width, frameSize.Height);
          dashedLayer.Bounds = shapeRect;
          dashedLayer.Position = new CGPoint(frameSize.Width / 2, frameSize.Height / 2);
          dashedLayer.FillColor = UIColor.Clear.CGColor;
          dashedLayer.StrokeColor = ColorUtils.ColorWithHex(ColorConstants.DarkBlue).CGColor;
          dashedLayer.LineWidth = 2;
          dashedLayer.LineJoin = CAShapeLayer.JoinRound;
          NSNumber[] patternArray = {5,5};
          dashedLayer.LineDashPattern = Array;
          var path = new CGPath();
          path.MoveToPoint(CGPoint.Empty);
          path.AddLineToPoint(new CGPoint(frameSize.Width, 0));
          dashedLayer.Path = path;
          separatorView.Layer.AddSublayer(dashedLayer);
     }
    

提交回复
热议问题